Code generation method and device based on large language model, and electronic device

By performing structured processing and semantic decomposition on the original code, a multi-level index knowledge base is constructed. A two-stage retrieval method is used to generate clearly structured prompt words, which solves the problems of low code generation efficiency and low quality in existing technologies, and achieves efficient and accurate code generation.

CN122152284APending Publication Date: 2026-06-05启元实验室

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
启元实验室
Filing Date
2026-01-15
Publication Date
2026-06-05

AI Technical Summary

Technical Problem

Existing technologies suffer from low retrieval efficiency and poor quality in code generation scenarios. They lack fine-grained semantic annotation and indexing of code corpora, resulting in inconsistent logic in the generated results, fictitious code, or code that does not match the real running environment. Furthermore, lengthy contexts negatively impact efficiency.

Method used

By breaking down the original code into structured code blocks, a multi-level index knowledge base is built. A two-stage retrieval method combining category matching and semantic vector comparison is adopted to generate well-structured and semantically focused combination prompts to guide the large language model in generating code.

Benefits of technology

It improves the accuracy and efficiency of code generation, reduces model illusion, enhances the executability and context relevance of generated code, and adapts to complex development needs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122152284A_ABST
    Figure CN122152284A_ABST
Patent Text Reader

Abstract

The application provides a code generation method and device based on a large language model, and an electronic device. The code generation method comprises the following steps: determining a target code category corresponding to prompt information input by a user in a preset knowledge base; performing semantic vector retrieval in the knowledge base according to the target code category and the prompt information, to obtain at least one code block corresponding to the prompt information; performing logical flow disassembly on the prompt information, to obtain at least one instruction set; sorting the at least one code block by using the at least one instruction set, to construct a combined prompt word; and generating code corresponding to the prompt information based on the large language model and the combined prompt word. According to the embodiment of the application, the user demand is task-decomposed and semantically aligned with the structured code segment, the code information highly related to each step is extracted, the redundancy is eliminated, and the prompt word with clear structure and focused semantics is constructed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of artificial intelligence technology, and more specifically, to a code generation method and apparatus based on a large language model, an electronic device, and a non-transitory computer-readable storage medium. Background Technology

[0002] Against the backdrop of the rapid development of large-scale model technology, retrieval-enhanced generation has become a key means of connecting massive external knowledge bases with generative model capabilities. However, existing solutions still have significant shortcomings in adaptability to code generation scenarios.

[0003] Traditional retrieval augmentation methods typically store code corpora as raw, unprocessed code blocks, lacking fine-grained semantic annotations and indexes such as function call relationships, input / output tensor flow, and runtime environment configuration. This coarse-grained storage method struggles to accurately match the core intent of a developer's query, resulting in code snippets returned during the retrieval stage being disconnected from actual needs, directly impacting the quality of the context upon which the generative model relies.

[0004] Furthermore, current methods often embed the retrieved original code snippets entirely into the generated prompts without effectively filtering and extracting their structure and information. The code corpora themselves are characterized by their length, high formatting, and uneven redundancy distribution. Direct embedding can lead to problems such as distracted model attention and increased information noise, resulting in logical inconsistencies, fabricated code, or content that does not match the real-world operating environment—a phenomenon known as "model illusion." Simultaneously, lengthy contextual information consumes a significant portion of the input length, limiting the model's focus on key information and ultimately impacting the efficiency and quality of code generation. Summary of the Invention

[0005] This application proposes a code generation method and apparatus, electronic device, and non-transitory computer-readable storage medium based on a large language model to solve the problems of low code retrieval efficiency and low code quality in existing retrieval enhancement methods.

[0006] According to one aspect of this application, a code generation method based on a large language model is proposed, comprising: Using the prompts input by the user, the target code category corresponding to the prompts is determined in a preset knowledge base; Based on the target code category, semantic vector retrieval is performed in the knowledge base using the prompt information to obtain at least one code block corresponding to the prompt information; The prompt information is logically broken down to obtain at least one set of instructions; The at least one code block is sorted using the at least one set of instructions to construct a combined prompt word; Using the combined prompt words, and based on the large language model, code corresponding to the prompt information is generated.

[0007] According to some embodiments, before determining the target code category corresponding to the user-input prompt information in a preset knowledge base, the method further includes: The knowledge base is constructed using the pre-defined source code.

[0008] According to some embodiments, the knowledge base is constructed using preset source code, including: Based on the execution order of the original code, the original code is broken down into at least one code block; Construct the target code category corresponding to the at least one code block in the knowledge base; The at least one code block is stored in the target code category corresponding to the at least one code block.

[0009] According to some embodiments, using user-input prompts, the target code category corresponding to the prompts is determined in a preset knowledge base, including: The prompt information is classified by keyword extraction, thereby determining the target code category corresponding to the prompt information in the knowledge base.

[0010] According to some embodiments, based on the target code category, semantic vector retrieval is performed using the prompt information to obtain at least one code block corresponding to the prompt information, including: Based on the target code category, semantic vector retrieval is performed using the prompt information, and at least one code block corresponding to the prompt information is selected from the target code category based on cosine similarity.

[0011] According to some embodiments, the at least one code block is sorted using the at least one set of instructions to construct a combined prompt word, including: The at least one code block is sorted using the at least one set of instructions; The at least one set of instructions is aligned and concatenated with the sorted at least one code block to construct the combined prompt word.

[0012] According to some embodiments, the original code includes pseudocode or generic structure code. Constructing the knowledge base using pre-defined source code includes: Based on the execution order of the pseudocode or the general structure code, the pseudocode or the general structure code is decomposed into at least one code block; Using the pseudocode or the general structure code, a default code category is constructed in the knowledge base; The at least one code block is stored in the default code category.

[0013] According to one aspect of this application, a code generation device based on a large language model is proposed, comprising: a target code category determination unit, used to determine the target code category corresponding to the prompt information in a preset knowledge base using prompt information input by a user; a retrieval unit, used to perform semantic vector retrieval in the knowledge base according to the target code category and using the prompt information to obtain at least one code block corresponding to the prompt information; a decomposition unit, used to decompose the prompt information into a logical flow to obtain at least one instruction set; a combined prompt word framework unit, used to sort the at least one code block using the at least one instruction set to construct combined prompt words; and a code generation unit, used to generate code corresponding to the prompt information based on the large language model using the combined prompt words.

[0014] According to one aspect of this application, an electronic device is provided, characterized in that it includes: a processor; a memory for storing a computer program; and when the computer program is executed by the processor, causing the processor to perform the method as described in any of the preceding embodiments.

[0015] According to one aspect of this application, a non-transitory computer-readable storage medium is provided, having stored thereon computer-readable instructions that, when executed by a processor, cause the processor to perform the method as described in any of the preceding embodiments.

[0016] According to the example embodiments of this application, by decomposing user requirements into tasks and semantically aligning them with structured code snippets, code information highly relevant to each step is extracted, redundancy is eliminated, key points are emphasized, and prompts with clear structure and focused semantics are constructed. Attached Figure Description

[0017] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below.

[0018] Figure 1a A code generation system architecture diagram based on a large language model is shown according to an example embodiment of this application.

[0019] Figure 1b A schematic diagram of a code structure modeling and knowledge base construction process according to an example embodiment of this application is shown.

[0020] Figure 1c A schematic diagram of a prompt word optimization process according to an example embodiment of this application is shown.

[0021] Figure 2 A flowchart of a code generation method based on a large language model according to an example embodiment of this application is shown.

[0022] Figure 3a A schematic diagram of the original code of a ResNet18 model training script according to an example embodiment of this application is shown.

[0023] Figure 3b An example diagram of a prompt word for parsing source code according to an example embodiment of this application is shown.

[0024] Figure 3c A schematic diagram of parsing code corresponding to the original code according to an example embodiment of this application is shown.

[0025] Figure 4 A schematic diagram of a target code category according to an example embodiment of this application is shown.

[0026] Figure 5a Another original code diagram according to an example embodiment of this application is shown.

[0027] Figure 5b A schematic diagram of parsing code corresponding to the original code according to an example embodiment of this application is shown.

[0028] Figure 5c A schematic diagram of another type of object code according to an example embodiment of this application is shown.

[0029] Figure 6a This illustrates a large model prompt word corresponding to a clearly targeted prompt word according to an example embodiment of this application.

[0030] Figure 6b A schematic diagram of a target code category according to an example embodiment of this application is shown.

[0031] Figure 6c A schematic diagram of a retrieved code block is shown according to an example embodiment of this application.

[0032] Figure 7a This illustrates another large model prompt word corresponding to a clearly targeted prompt word according to an example embodiment of this application.

[0033] Figure 7b A schematic diagram of another type of object code according to an example embodiment of this application is shown.

[0034] Figure 7c A schematic diagram of another retrieved code block is shown according to an example embodiment of this application.

[0035] Figure 8a A schematic diagram of prompts illustrating the breakdown of completion steps according to an example embodiment of this application is shown.

[0036] Figure 8b A schematic diagram of a step sorting prompt word according to an example embodiment of this application is shown.

[0037] Figure 8c A schematic diagram of a code block for completing sorting is shown according to an example embodiment of this application.

[0038] Figure 8d A schematic diagram of a combined prompt word according to an example embodiment of this application is shown.

[0039] Figure 9a A schematic diagram of prompts illustrating another breakdown of the completion steps according to an example embodiment of this application is shown.

[0040] Figure 9b A schematic diagram illustrating another step of sorting prompt words according to an example embodiment of this application is shown.

[0041] Figure 9c A schematic diagram of another code block for completing the sorting is shown according to an example embodiment of this application.

[0042] Figure 9d A schematic diagram of another combination of prompt words according to an example embodiment of this application is shown.

[0043] Figure 10 A block diagram of a code generation apparatus based on a large language model according to an example embodiment of this application is shown.

[0044] Figure 11 An electronic device is shown according to an exemplary embodiment of this application. Detailed Implementation

[0045] Exemplary embodiments will now be described more fully with reference to the accompanying drawings. However, these exemplary embodiments can be implemented in many forms and should not be construed as limited to the embodiments set forth herein; rather, they are provided so that this application will be thorough and complete, and will fully convey the concept of the exemplary embodiments to those skilled in the art. The same reference numerals in the drawings denote the same or similar parts, and therefore repeated descriptions of them will be omitted.

[0046] The described features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Numerous specific details are provided in the following description to give a full understanding of embodiments of this disclosure. However, those skilled in the art will recognize that the technical solutions of this disclosure can be practiced without one or more of these specific details, or other methods, components, materials, apparatus, or operations may be employed. In these cases, well-known structures, methods, apparatuses, implementations, materials, or operations will not be shown or described in detail.

[0047] The flowcharts shown in the accompanying drawings are merely illustrative and do not necessarily include all content and operations / steps, nor do they necessarily have to be performed in the described order. For example, some operations / steps can be broken down, while others can be combined or partially combined; therefore, the actual execution order may change depending on the specific circumstances.

[0048] The terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish different objects, not to describe a specific order. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to these processes, methods, products, or apparatuses.

[0049] In existing technologies, code knowledge bases typically store raw code blocks at the function level. This approach lacks the ability to model the internal structure and semantic relationships of the code, and cannot identify function call relationships, runtime environment configuration information, or conditional dependencies of key logical branches. When faced with code generation tasks involving real-world scenarios, the returned code snippets often fail to meet the core intent of the queryer. Furthermore, since the raw code blocks themselves usually do not come with any static analysis tags or contextual annotations, the model struggles to understand the true purpose and boundary conditions of the code snippets during the generation phase, affecting the accuracy and reliability of the generated code.

[0050] Furthermore, in existing retrieval augmentation systems, retrieved code snippets are typically embedded as a whole into the input prompts of the generative model, lacking structured filtering, summarization, and importance ranking mechanisms. This approach is extremely unfriendly to highly structured and redundant code text, especially when dealing with deep learning model code. A typical function may contain numerous initialization, configuration, and logging statements unrelated to the core logic. Embedding the entire training loop code into the generated prompts significantly consumes context window resources, causing truly critical information (such as function call relationships or environment configuration parameters) to be compressed or ignored. In addition, direct injection of unprocessed code snippets can introduce semantic conflicts or incorrect dependencies, such as variable scope conflicts and incorrect library function calls, leading to logical inconsistencies, broken call chains, or "model illusion" problems such as fictitious code modules during model generation. This problem is particularly severe when building runnable and highly maintainable generated code, and has become one of the main bottlenecks restricting the performance improvement of current code-oriented retrieval augmentation systems.

[0051] Figure 1a This paper illustrates an architecture diagram of a code generation system based on a large language model according to an example embodiment of this application, such as... Figure 1a The code generation system shown includes a knowledge base construction unit, a code sample retrieval unit, and a prompt word optimization unit.

[0052] According to embodiments of this application, the knowledge base construction unit constructs a multi-level indexed knowledge base by parsing the original code into a structured representation containing step words and code fields.

[0053] In a specific embodiment, the knowledge base construction unit transforms the original code files into structured, searchable semantic units and constructs a multi-level organized code knowledge base. As shown in Figure 1, the knowledge base construction unit includes an original code file preprocessing subunit and a code sample classification and indexing subunit. The original code file preprocessing subunit preprocesses the complete code file, converting it into a standardized JSON format containing "step" and "code" fields according to the actual execution logic, thus clarifying the specific implementation content corresponding to each step. Subsequently, the generated JSON file is vectorized and stored in a database to support efficient semantic retrieval.

[0054] Based on this, the code sample classification and indexing subunit classifies, organizes, and indexes structured code samples according to the logical hierarchy of the code during operation (including but not limited to specific algorithms, task types, deployment platforms, etc.).

[0055] In some embodiments, the knowledge base construction module also introduces representative default template content for each logical level. For example, a general training process template can be added to the codebase, or a general configuration template can be provided in a codebase for a specific deployment architecture to enhance the universality and coverage of the knowledge base. The introduction of default templates can not only enhance the accuracy of generated code in scenarios where exact matching exists, but also ensure that the generated code meets basic runtime environment requirements in cases where direct matching is lacking, thereby effectively improving code executability and overall system robustness.

[0056] According to an embodiment of this application, the code sample retrieval unit adopts a two-stage retrieval method of category matching and semantic vector comparison to filter the structured code fragments most relevant to the requirements.

[0057] like Figure 1a As shown, the code sample retrieval unit includes a prompt keyword extraction and classification subunit and a prompt semantic vector retrieval resource subunit. The prompt keyword extraction and classification subunit constructs prompt words based on the user's original input, combines them with the hierarchical index list built from the code knowledge base, extracts and classifies the prompt words, and generates retrieval category keywords corresponding to the knowledge base categories. Using this keyword set, it performs category matching in the knowledge base to filter out the target code category closest to the user's needs, completing the first stage of candidate scope limitation, thereby reducing the computational complexity of subsequent matching and improving the accuracy of the initial retrieval. The prompt semantic vector retrieval resource subunit vectorizes the user's prompt words, converting them into semantic embedding representations, and combines them with the structured code fragments already saved in the candidate categories, especially the semantic description of their "step" field, to perform vector retrieval. By calculating semantic similarity, it selects several code fragments most relevant to the user's intent from the candidate structured units as the final retrieval results for use by the subsequent generation module. This embodiment effectively improves matching accuracy and retrieval efficiency through a two-stage retrieval mechanism of "coarse-grained keyword classification + fine-grained semantic matching", ensuring that the provided context semantically matches the user's needs.

[0058] The prompt word optimization unit generates target code by constructing prompt words based on task chain. For example, it performs structured understanding and step-by-step reconstruction of the natural language requirements input by the user, and combines the reference code fragments returned by the semantic retrieval module to generate semantically clear and highly adaptable large model input prompt words, so as to improve the accuracy, completeness and controllability of the output code of the generation module.

[0059] like Figure 1a As shown, the prompt word optimization unit includes a prompt word task decomposition subunit, a candidate code block sorting subunit, and an input prompt word combination subunit.

[0060] The prompt word task decomposition subunit is used to semantically parse the user's original natural language requirements, extract the core intent, and decompose the task to generate a hierarchical, refined operation chain. This operation chain represents the user's overall intent in a "demand chain" structure, breaking down complex requirements into a series of logically sequential subtasks. This facilitates subsequent code snippet selection and prompt word organization, improving the contextual fit of the generated code.

[0061] The candidate code block sorting subunit is used to compare the steps of each subtask obtained from the above decomposition with the structured code fragments returned by the semantic retrieval module. It focuses on analyzing the execution semantic information of the "step" field in the structured fragments, and sorts the candidate code blocks according to their relevance. It prioritizes the code that is most similar to the current subtask in function and semantics as a reference, thereby enhancing the context matching degree between the retrieved code block and the generated prompt words.

[0062] Based on task decomposition and matching ranking, the input prompt word combination subunit constructs combined prompt words that can be directly accepted by the large model. In a specific embodiment, the input prompt word combination subunit merges and organizes the ranked structured reference code fragments with the optimized requirement steps to form a prompt such as "Combined with the following reference code, complete the implementation of function X," where "reference code" corresponds to the filtered and reorganized structured code, and "function X" is derived from the summary expression of the task decomposition results.

[0063] In some embodiments, the resulting prompt words are used to drive the large model to generate target code that meets expectations.

[0064] according to Figure 1a The illustrated embodiment supports structured semantic annotation and fine-grained indexing of the code knowledge base during the knowledge base construction phase, and enables structured filtering and organization of retrieval results to improve the generative model's responsiveness to real and complex development needs, making code snippet retrieval more accurate. At the same time, combined with the prompt word construction strategy, the retrieval content is reorganized during the retrieval phase, which can effectively reduce redundant noise, alleviate model illusion, and thus significantly improve the context relevance, correctness, and executability of code generation, enhancing the system's adaptability and practical value in engineering scenarios.

[0065] It should be noted here that, from a "hardware" perspective, Figure 1a The system shown is deployed on a general-purpose high-performance computing platform and has certain requirements for computing resources.

[0066] First, sufficient memory resources are required to handle the large amounts of structured code units, semantic vectors, and retrieval indexes during knowledge base construction. Second, the system demands high processor computing power, especially when performing computationally intensive tasks such as vectorization processing, semantic similarity calculation, and large model inference. It needs a computing environment with multi-core CPUs or accelerated computing units (such as GPUs or NPUs) to support low-latency, high-throughput service response capabilities. Furthermore, a stable network environment is crucial for efficient communication in distributed retrieval and large model interface calls.

[0067] From a "software" perspective, the implementation process of the system shown in Figure 1 includes: Code structured modeling and knowledge base construction process: such as Figure 1b As shown, the system semantically decomposes the original code through a large model interface, transforming it into JSON format containing "steps" and "code" fields. It then performs vectorization processing and categorizes the data into the database according to dimensions such as algorithm type, task category, and deployment platform. Simultaneously, default templates are added to each level to improve the knowledge base coverage.

[0068] Code sample retrieval and screening process: such as Figure 1c As shown, after receiving user requests, the system extracts keywords and search categories for the first step of category matching and filtering. Subsequently, it performs semantic vector comparison between the user input and candidate codes, selecting the most relevant structured fragments as contextual references.

[0069] Prompt word optimization generation process: such as Figure 1c As shown, the system breaks down user requirements into operational steps and matches and sorts them with the semantic information of the reference code. Finally, it constructs a prompt containing "reference code + target goal," which is input into the large model to guide the generation of the target code.

[0070] The specific embodiments according to this application will now be described in detail with reference to the accompanying drawings.

[0071] Figure 2 A flowchart illustrating a code generation method based on a large language model according to an example embodiment of this application is shown, such as... Figure 2 The code generation methods shown include In step S201, the target code category corresponding to the prompt information is determined in a preset knowledge base using the prompt information input by the user.

[0072] According to an embodiment of this application, before step S201, the knowledge base is constructed using preset source code.

[0073] In some embodiments, when constructing the knowledge base using preset source code, firstly, the source code is decomposed into at least one code block according to the execution order of the source code; then, a target code category corresponding to the at least one code block in the knowledge base is constructed; finally, the decomposed at least one code block is stored in the target code category corresponding to the at least one code block.

[0074] In other embodiments, when using user-input prompts to determine the target code category corresponding to the prompts in a preset knowledge base, the prompts are first classified by keyword extraction, thereby determining the target code category corresponding to the prompts in the knowledge base.

[0075] In a specific embodiment, when constructing the knowledge base using preset source code, firstly, the complete source code file is preprocessed. Based on the actual execution logic, the source code is converted into a standardized JSON format containing "step" and "code" fields to clarify the specific implementation content corresponding to each step. Subsequently, the generated JSON file is vectorized and stored in the database.

[0076] For example, suppose a developer uploads a ResNet18 model training script for an image classification task, such as... Figure 3a As shown. When constructing the knowledge base using the preset source code, firstly, the source code is semantically parsed using pre-constructed system prompts, such as... Figure 3b The image shows an example of prompts used to parse the original code; after packaging the original code, the system will pass in the corresponding model interface to obtain the returned processing code.

[0077] In some embodiments, the returned code includes a portion that breaks down the original code into multiple steps in the actual execution order and transforms them into a standardized JSON format. For example... Figure 3c As shown, each step contains two fields: "step" and "code". The content of "step" is a natural language description of the function of that code snippet.

[0078] In other embodiments, after obtaining the structured JSON sample, it is also necessary to automatically construct its category path in the code knowledge base based on the metadata information of the code sample. For example, the constructed category path is a three-level structure of "algorithm type → model structure → development framework". Figure 4 The image shown is the final category path obtained for the ResNet18 image classification model.

[0079] According to embodiments of this application, the original code includes pseudocode or general structure code.

[0080] In some embodiments, when constructing the knowledge base using preset source code, firstly, the pseudocode or the general structure code is decomposed into at least one code block according to the execution order of the pseudocode or the general structure code; then, a default code category is constructed in the knowledge base using the pseudocode or the general structure code; and then, the at least one code block is stored in the default code category.

[0081] For example, when a user request lacks explicit model structure information, or when the corresponding sample cannot be retrieved from the knowledge base, the default template code will be used to provide the most basic contextual structure guidance for generating the model.

[0082] In specific embodiments, such default templates do not contain specific algorithm logic or model definitions, but express the general process of task execution in the form of pseudocode or general structure code, ensuring that the generated code has structural integrity and environment configurability.

[0083] For example, such as Figure 5a As shown, taking the image classification task in the PyTorch framework as an example, its default template content is written in the PyTorch framework style, but no specific model is specified, only basic structured statements such as training loops, loss calculations, and optimizer calls are retained. By using methods such as... Figure 3b The indicated prompt words are for Figure 5a The original code shown is preprocessed to obtain the following: Figure 5b The structured sample shown.

[0084] In some embodiments, default structured templates are categorized in the knowledge base under the identifier "default," independent of the model structure name or algorithm type, and associated only with the task category (e.g., image classification) and framework environment (e.g., PyTorch). Figure 5c As shown.

[0085] In step S203, based on the target code category, semantic vector retrieval is performed in the knowledge base using the prompt information to obtain at least one code block corresponding to the prompt information.

[0086] According to some embodiments, in step S205, firstly, semantic vector retrieval is performed using the prompt information based on the target code category; then, at least one code block corresponding to the prompt information is selected from the target code category based on cosine similarity.

[0087] For example, suppose a user inputs a specific prompt into the large model: "Please help me build an image classification training script using ResNet18, based on the PyTorch framework." According to an embodiment of this application, the currently registered directory structure in the knowledge base is first read based on the input prompt, and this structure, along with the user's request, is input into the large model, requesting it to return the most suitable keyword path, i.e., the target code category. The prompt input into the large model is as follows: Figure 6a As shown. The results returned by the large model are as follows. Figure 6b As shown, the search path "PyTorch / Image Classification / ResNet18" can be obtained for the next step of semantic retrieval. Afterwards, within this directory path, an embedding model (such as CodeBERT, SimCSE, or a proprietary semantic encoder) is used to perform semantic embedding on each "step" field, and the user's requirements are also converted into a vector representation. The most relevant code blocks are then selected using cosine similarity scoring. Assuming the top four most relevant code blocks (topk=4) are selected, the final obtained code blocks are as follows... Figure 6c As shown.

[0088] For example, suppose a user inputs a prompt without a specific target into a large model, such as "Please help me write a training process for an image classification model, implemented using PyTorch." According to embodiments of this application, the currently registered directory structure in the knowledge base will be read first, and this structure, along with the prompt without a specific target (such as...), will be... Figure 7a (As shown) These are input together into the large model, which is then asked to return the most suitable keyword path. The result returned by the large model is as follows. Figure 7b As shown in the image. Next, navigate to this directory path and use an embedding model (such as CodeBERT, SimCSE, or a proprietary semantic encoder) to perform semantic embedding on the `step` field of each step. Also, convert the user requirements into a vector representation. Use cosine similarity scoring to filter out the most relevant code blocks. Assuming the top four most relevant code blocks (topk=4) are selected, the final code blocks obtained are as follows: Figure 7c As shown.

[0089] In step S205, the prompt information is logically decomposed to obtain at least one set of instructions.

[0090] According to an embodiment of this application, in step S205, the original natural language request input by the user is semantically parsed, the core intent is extracted and decomposed into tasks, generating a refined operation chain with a hierarchical structure to obtain at least one set of instructions. The operation chain represents the user's overall intent in a "demand chain" structure, breaking down complex requirements into a series of sub-tasks with logical sequence relationships. This facilitates subsequent code snippet selection and prompt word organization, improving the contextual fit of the generated code.

[0091] In step S207, the at least one code block is sorted using the at least one set of instructions to construct a combined prompt word.

[0092] According to an embodiment of this application, in step S207, firstly, the at least one code block is sorted using the at least one instruction set; then, the at least one instruction set and the sorted at least one code block are aligned and concatenated to construct the combined prompt word.

[0093] For example, suppose the user inputs the prompt "Please help me build an image classification training script using ResNet18, based on the PyTorch framework." According to an embodiment of this application, the large model interface is called to semantically rewrite the input statement, breaking down the operation flow into multiple instructions in logical order. This process is similar to natural language planning: the large model predicts the standard process of building a training script based on keywords (such as ResNet18, training, PyTorch). Each instruction maintains a natural language style and carries semantic boundaries to facilitate subsequent structural alignment. The large model returns the prompts for completing the step decomposition, such as... Figure 8a As shown.

[0094] Since the code blocks returned in step S203 are sorted by similarity, their logical order may not be entirely consistent with the training script. In step S207, the instruction set decomposed in step S205 will be used to call the large model interface to determine dependencies and reorder the code snippets. Figure 8a Correspondingly, inputting into the large model such as Figure 8b The prompts shown lead to the following results: Figure 8c A diagram illustrating the code block used to complete the sorting process.

[0095] Ultimately, the system will... Figure 8a The natural language task chain shown is similar to... Figure 8c The sorted structured code snippets are aligned and concatenated. Similar to template filling, the module reads the task description and corresponding code sequentially, combining them into segmented instruction prompts. The template format is, for example, "Complete task X, refer to the following code: Y". Alignment is maintained within each segment, while segments are connected by natural language descriptions in the "step" field to ensure contextual coherence, resulting in... Figure 8d The example shown is a search enhancement suggestion.

[0096] For example, suppose the user prompt is "Please help me write a training process for an image classification model, implemented using PyTorch." According to the embodiments of this application, the large model interface is called to semantically rewrite the input statement, breaking down the operation flow into multiple instructions in logical order. The large model returns a prompt indicating the completion of the decomposed steps, such as... Figure 9a As shown.

[0097] Next, using the instruction set extracted in step S205, the large model interface will be called to determine dependencies and reorder the code snippets. Figure 9a Correspondingly, inputting into the large model such as Figure 9b The prompts shown lead to the following results: Figure 9c A diagram illustrating the code block used to complete the sorting process.

[0098] Ultimately, by Figure 9a The natural language task chain shown is similar to... Figure 9c After sorting the structured code snippets, align and concatenate them to obtain, as shown below. Figure 9d The example shown is a search enhancement suggestion.

[0099] In step S209, using the combined prompt words, code corresponding to the prompt information is generated based on the large language model.

[0100] According to an embodiment of this application, in step S209, the obtained combined prompt words are input into the large model, and actual code generation is performed based on the large language model.

[0101] It should be noted that the large model mentioned in this application refers to a machine learning model with a large number of parameters and powerful computing capabilities. This application does not limit the type of large model; any large model that can accomplish the tasks of this application is applicable to the embodiments of this application.

[0102] according to Figure 2 The illustrated embodiment decomposes user requirements into tasks and semantically aligns them with structured code snippets, extracting code information highly relevant to each step, eliminating redundancy, highlighting key points, and constructing clearly structured and semantically focused prompts. This refined organization not only improves the model's efficiency in utilizing retrieved code blocks, effectively reduces attention distraction and contextual redundancy, and enhances the logical consistency and runtime usability of generated code, but also effectively alleviates the model illusion and generation chaos caused by "directly embedding lengthy code snippets" in existing methods. The embodiments of this application are particularly suitable for code generation tasks in real-world development environments.

[0103] According to other embodiments, by structuring the code corpus and vectorizing it according to execution semantics, accurate matching of user query intent can be achieved. Compared with directly storing the original code blocks, structured semantic indexing can better capture key contexts such as function dependencies, input / output processes, and environment configurations, significantly improving the relevance of search results to user needs. At the same time, the retrieval process of first locating categories and then comparing semantics significantly shortens the response time, ensuring fast and accurate retrieval capabilities even when facing complex queries and knowledge base structures, and solving the problems of coarse granularity and lack of semantic structure annotation in traditional knowledge base retrieval methods.

[0104] The above description primarily focuses on the methodological aspects of the embodiments of this application. Those skilled in the art should readily recognize that, based on the operations or steps described in conjunction with the embodiments disclosed herein, this application can be implemented in hardware or a combination of hardware and computer software. Those skilled in the art can implement the described functionality in different ways for each specific operation or method, and such implementations should not be considered beyond the scope of this application.

[0105] The apparatus embodiments of this application are described below. For details not described in the apparatus embodiments of this application, please refer to the method embodiments of this application.

[0106] Figure 10 A block diagram of a code generation apparatus based on a large language model according to an example embodiment of this application is shown, such as Figure 10 The code generation device shown includes a target code category determination unit 1001, a retrieval unit 1003, a decomposition unit 1005, a combined prompt word framework unit 1007, and a code generation unit 1009. Specifically, the target code category determination unit 1001 uses user-input prompt information to determine the target code category corresponding to the prompt information in a preset knowledge base; the retrieval unit 1003 performs semantic vector retrieval in the knowledge base based on the target code category and the prompt information to obtain at least one code block corresponding to the prompt information; the decomposition unit 1005 decomposes the prompt information logically to obtain at least one instruction set; the combined prompt word framework unit 1007 sorts the at least one code block using the at least one instruction set to construct combined prompt words; and the code generation unit 1009 uses the combined prompt words, based on the large language model, to generate code corresponding to the prompt information.

[0107] Figure 11 An electronic device according to an exemplary embodiment of this application is shown. Reference is made below. Figure 11 To describe an electronic device 200 according to this embodiment of the present application. Figure 11The electronic device 200 shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.

[0108] like Figure 11 As shown, the electronic device 200 is presented in the form of a general-purpose computing device. The components of the electronic device 200 may include, but are not limited to: at least one processing unit 210, at least one storage unit 220, a bus 230 connecting different system components (including storage unit 220 and processing unit 210), a display unit 240, etc.

[0109] The storage unit stores program code that can be executed by the processing unit 210, causing the processing unit 210 to perform the methods described in this specification according to various exemplary embodiments of this application. For example, the processing unit 210 can perform the methods described above.

[0110] Storage unit 220 may include readable media in the form of volatile storage units, such as random access memory (RAM) 2201 and / or cache memory 2202, and may further include read-only memory (ROM) 2203.

[0111] Storage unit 220 may also include a program / utility 2204 having a set (at least one) program module 2205, such program module 2205 including but not limited to: operating system, one or more application programs, other program modules and program data, each or some combination of these examples may include an implementation of a network environment.

[0112] Bus 230 can represent one or more of several types of bus structures, including a memory cell bus or memory cell controller, a peripheral bus, a graphics acceleration port, a processing unit, or a local bus using any of the various bus structures.

[0113] Electronic device 200 can also communicate with one or more external devices 300 (e.g., keyboard, pointing device, Bluetooth device, etc.), and with one or more devices that enable a user to interact with electronic device 200, and / or with any device that enables electronic device 200 to communicate with one or more other computing devices (e.g., router, modem, etc.). This communication can be performed via input / output (I / O) interface 250. Furthermore, electronic device 200 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via network adapter 260. Network adapter 260 can communicate with other modules of electronic device 200 via bus 230. It should be understood that, although not shown in the figures, other hardware and / or software modules can be used in conjunction with electronic device 200, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.

[0114] Through the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. The technical solutions according to the embodiments of this application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, or network device, etc.) to execute the methods described above according to the embodiments of this application.

[0115] Software products may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: electrical connections with one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.

[0116] Computer-readable storage media may include data signals propagated in baseband or as part of a carrier wave, carrying readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable storage medium may also be any readable medium other than a readable storage medium that can transmit, propagate, or transfer a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof.

[0117] Program code for performing the operations of this application can be written in any combination of one or more programming languages, including object-oriented programming languages ​​such as Java and C++, and conventional procedural programming languages ​​such as C or similar languages. The program code can execute entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).

[0118] The aforementioned computer-readable medium carries one or more programs, which, when executed by a device, cause the computer-readable medium to perform the aforementioned functions.

[0119] Those skilled in the art will understand that the above modules can be distributed in the device as described in the embodiments, or they can be modified accordingly and placed in one or more devices that are unique to this embodiment. The modules in the above embodiments can be combined into one module, or they can be further divided into multiple sub-modules.

[0120] According to an embodiment of this application, a computer program is proposed, including a computer program or instructions, which, when executed by a processor, can perform the methods described above.

[0121] The embodiments of this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are only for the purpose of helping to understand the methods and core ideas of this application. Furthermore, any changes or modifications made by those skilled in the art based on the ideas of this application, the specific implementation methods, and the application scope of this application, are all within the scope of protection of this application. In summary, the content of this specification should not be construed as a limitation of this application. Those skilled in the art will understand that the above modules can be distributed in the device as described in the embodiments, or can be modified accordingly to be uniquely different from one or more devices in this embodiment. The modules of the above embodiments can be combined into one module, or can be further divided into multiple sub-modules.

[0122] The embodiments of this application have been described in detail above. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are only for the purpose of helping to understand the method and core ideas of this application. Furthermore, any changes or modifications made by those skilled in the art based on the ideas of this application, and on the specific implementation methods and application scope of this application, are all within the scope of protection of this application. Therefore, the content of this specification should not be construed as a limitation of this application.

Claims

1. A code generation method based on a large language model, characterized in that, include: Using the prompts input by the user, the target code category corresponding to the prompts is determined in a preset knowledge base; Based on the target code category, semantic vector retrieval is performed in the knowledge base using the prompt information to obtain at least one code block corresponding to the prompt information; The prompt information is logically broken down to obtain at least one set of instructions; The at least one code block is sorted using the at least one set of instructions to construct a combined prompt word; Using the combined prompt words, and based on the large language model, code corresponding to the prompt information is generated.

2. The code generation method according to claim 1, characterized in that, Before determining the target code category corresponding to the user-input prompts in a preset knowledge base, the method further includes: The knowledge base is constructed using the pre-defined source code.

3. The code generation method according to claim 2, characterized in that, Constructing the knowledge base using pre-defined source code includes: Based on the execution order of the original code, the original code is broken down into at least one code block; Construct the target code category corresponding to the at least one code block in the knowledge base; The at least one code block is stored in the target code category corresponding to the at least one code block.

4. The code generation method according to claim 3, characterized in that, Using user-input prompts, the target code category corresponding to the prompts is determined in a preset knowledge base, including: The prompt information is classified by keyword extraction, thereby determining the target code category corresponding to the prompt information in the knowledge base.

5. The code generation method according to claim 4, characterized in that, Based on the target code category, semantic vector retrieval is performed using the prompt information to obtain at least one code block corresponding to the prompt information, including: Based on the target code category, semantic vector retrieval is performed using the prompt information, and at least one code block corresponding to the prompt information is selected from the target code category based on cosine similarity.

6. The code generation method according to claim 5, characterized in that, The at least one code block is sorted using the at least one set of instructions to construct a combined prompt word, including: The at least one code block is sorted using the at least one set of instructions; The at least one set of instructions is aligned and concatenated with the sorted at least one code block to construct the combined prompt word.

7. The code generation method according to claim 2, characterized in that, The original code includes pseudocode or generic structure code. Constructing the knowledge base using pre-defined source code includes: Based on the execution order of the pseudocode or the general structure code, the pseudocode or the general structure code is decomposed into at least one code block; Using the pseudocode or the general structure code, a default code category is constructed in the knowledge base; The at least one code block is stored in the default code category.

8. A code generation device based on a large language model, characterized in that, include: The target code category determination unit is used to determine the target code category corresponding to the prompt information in a preset knowledge base using the prompt information input by the user. The retrieval unit is configured to perform semantic vector retrieval in the knowledge base based on the target code category and the prompt information, so as to obtain at least one code block corresponding to the prompt information; The disassembly unit is used to logically disassemble the prompt information to obtain at least one set of instructions. A combined prompt word framework unit is used to sort the at least one code block using the at least one instruction set to construct combined prompt words; The code generation unit is used to generate code corresponding to the prompt information based on the combined prompt words and the large language model.

9. An electronic device, characterized in that, include: processor; Memory, used to store computer programs; When the computer program is executed by the processor, the processor causes the processor to implement the code generation method as described in any one of claims 1-7.

10. A non-transitory computer-readable storage medium having stored thereon computer-readable instructions that, when executed by a processor, cause the processor to perform the code generation method as described in any one of claims 1-7.