Code generation method and device and computing equipment

By utilizing private domain code snippets and question-answering models in RAG technology, user questions are analyzed and weight values ​​are adjusted to generate code that better matches user preferences. This solves the problem of code generation not meeting user needs in existing technologies, and improves user experience and generation efficiency.

CN120848853APending Publication Date: 2025-10-28XFUSION DIGITAL TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510906889.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-01
Publication Date
2025-10-28

AI Technical Summary

Technical Problem

Existing code generation methods based on RAG technology rely on large open-source code libraries and lack a deep understanding of user preferences, resulting in a gap between the generated code and actual needs, which affects the user experience.

Method used

By obtaining questions input by users, using preset snippets to analyze and adjust the weight values ​​of question snippets, obtaining the code snippets most relevant to user needs from private domain code snippets, and using the question-answering model to generate target code, ensuring that the code meets user preferences.

Benefits of technology

It improves the accuracy of code generation and user experience, ensuring that the generated code better meets the actual needs of users, thereby improving processing efficiency and user satisfaction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120848853A_ABST
    Figure CN120848853A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code generation method and device and computing equipment. The method comprises the steps of obtaining a first question input by a user; the first question is used for representing description content required by a user; determining a weight value of each question fragment based on a matching result of at least one question fragment contained in the first question and a preset fragment; generating a second question; wherein the second question comprises at least one selected fragment selected from the question fragments and a weight value of the selected fragment; obtaining a first code snippet from the at least one private domain code snippet based on the second question; wherein the private domain code snippets are code snippets with preset annotation prompt words; the weight value of the first code snippet is matched with the weight value of the selected snippet; and taking the first code snippet and the first question as input of a question-answer model, and utilizing the question-answer model to obtain a target code. By means of the method, it can be guaranteed that the generated code better conforms to the preference of the user, and the user experience is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of code development technology, and in particular to a code generation method, apparatus and computing device. Background Technology

[0002] With the rapid development of information technology, artificial intelligence (AI) assistants play a vital role in code development, helping users complete programming tasks such as generating code snippets and interpreting error messages. The support of AI assistants greatly simplifies the programming process. However, when faced with complex or highly specialized programming tasks, the code generated by AI assistants often has limitations, such as insufficient information.

[0003] To address this issue, retrieval-augmented generation (RAG) technology has been applied to the field of code generation. Its workflow mainly consists of two key steps: first, retrieving information relevant to user needs from a large codebase; and then combining this retrieved information with a generative model to generate more accurate code.

[0004] However, while the aforementioned code generation method based on RAG technology can improve the accuracy of code generation, the large codebases that this method relies on are usually composed of open-source code and general technical documents in the public domain. These general code data lack a deep understanding of user preferences, such as personalized programming styles and habits, resulting in a certain gap between the generated code and actual needs, thus affecting the overall user experience. Summary of the Invention

[0005] This application provides a code generation method that ensures the generated code better matches user preferences and improves the overall user experience.

[0006] To achieve the above objectives, the embodiments of this application adopt the following technical solutions:

[0007] In a first aspect, embodiments of this application provide a code generation method, the method comprising: obtaining a first question input by a user; the first question being a descriptive content used to characterize user needs; determining a weight value for each question fragment based on the matching result of at least one question fragment contained in the first question with a preset fragment; generating a second question; wherein the second question includes at least one selected fragment selected from the question fragments and a weight value for the selected fragment; obtaining a first code fragment from at least one private domain code fragment based on the second question; wherein the private domain code fragment is a code fragment with preset labeled prompt words; the weight values ​​of the first code fragment and the selected fragment are matched; and using the first code fragment and the first question as input to a question-answering model, obtaining a target code using the question-answering model.

[0008] Based on this solution, by analyzing the user's first question using pre-set segments, the weight values ​​of key question segments are updated. Then, based on the updated first question (i.e., the second question), the most relevant first code segment with labeled prompts is precisely identified. Finally, the model is used to generate target code. This ensures that the generated target code better matches the user's preferences, thereby improving the overall user experience.

[0009] In another possible implementation, the weight value of a question segment is determined based on the matching result between the question segment contained in the first question and the preset segment, including: determining at least one question segment in the first question based on a partitioning rule; the partitioning rule is used to characterize the partitioning based on at least one of the structure, semantics and logical relationships of the user input question; determining at least one selected segment among the at least one question segment based on the preset segment; and adjusting the weight value of each selected segment to obtain the weight value of the question segment.

[0010] Based on this solution, problem segments are identified using segmentation rules. Then, based on preset segments, it determines which of these segments are key segments (i.e., selected segments). By adjusting their weight values, these selected segments better reflect user preferences during subsequent code generation. This improves the accuracy of subsequent matching and enhances the user experience.

[0011] In one possible implementation, the selected fragments include at least one first target fragment and / or at least one second target fragment. The first target fragment is a problem fragment that matches a preset fragment, and the second target fragment is a problem fragment that partially matches the preset fragment. The weight value of the first target fragment is higher than the weight value of the second target fragment. Based on this scheme, the selected fragments include problem fragments that completely match the preset fragment and problem fragments that partially match the preset fragment. Different weight values ​​are set for different degrees of matching. This allows for a more accurate capture of the core content of user needs, ensuring that the first code fragment generated based on these selected fragments better meets the user's actual needs, thereby improving the efficiency and accuracy of problem-solving.

[0012] In another possible implementation, based on the second question, obtaining the first code segment from at least one private domain code segment includes: obtaining the first code segment corresponding to the second question based on the similarity between the second question and each private domain code segment.

[0013] Based on this scheme, similarity calculation can filter out the first code fragment most relevant to the user's needs from multiple private domain code fragments. This ensures that the generated or retrieved code highly matches the user's actual needs, while avoiding invalid searches for irrelevant or low-relevance code fragments, thus improving processing efficiency.

[0014] In another possible implementation, the first code segment corresponding to the second problem is obtained based on the similarity between the second problem and each private domain code segment, including: obtaining the target feature vector corresponding to the second problem; calculating the similarity between the target feature vector and each first feature vector; the first feature vector is used to characterize the features of the private domain code segment; and obtaining the first code segment corresponding to the second problem based on at least one similarity.

[0015] Based on this scheme, the private domain code fragments are transformed into the first feature vector, and the second question is also transformed into a feature vector. The similarity between the two is then calculated. In this way, through vectorization and similarity calculation, the most relevant code fragments can be quickly filtered out from a large-scale private domain code fragment library, significantly improving retrieval efficiency.

[0016] In yet another possible implementation, the first problem includes at least one of the descriptive text and code.

[0017] Based on this solution, the content of the first question is not limited. In this way, no matter which form the user chooses to ask the question, they can get a targeted and high-quality answer, which greatly improves user satisfaction and experience.

[0018] In another possible implementation, before obtaining the first code segment from at least one private code segment based on the second problem, the method further includes: constructing a private code library; the private code library storing at least one private code segment with preset annotation prompts.

[0019] Based on this solution, a private code repository is built to centrally store annotated code snippets, thereby enabling rapid retrieval and efficient reuse. Simultaneously, with the help of preset annotation prompts, code snippets relevant to specific needs can be accurately located.

[0020] In another possible implementation, a private domain code database is constructed, including: parsing the private domain code file based on syntax nodes to obtain at least one code segment corresponding to the private domain code file; processing each code segment based on a preset annotation method and preset segments to obtain at least one code segment with preset annotation prompts; and storing at least one code segment with preset annotation prompts in the private domain code database.

[0021] Based on this solution, private domain code files are parsed using syntax nodes to extract corresponding code snippets. These snippets are then processed using preset annotation methods and preset snippets. Finally, code snippets with preset annotation prompts are stored in the private domain code repository. This improves the quality and efficiency of building the private domain code repository and lays a solid foundation for code reuse, retrieval, and intelligent applications.

[0022] Secondly, embodiments of this application also provide a code generation apparatus, comprising: an acquisition module configured to: acquire a first question input by a user; the first question being a descriptive content used to characterize user needs; a matching module configured to: determine a weight value for each question fragment based on the matching result of at least one question fragment contained in the first question with a preset fragment; a generation module configured to: generate a second question; wherein the second question includes at least one selected fragment selected from the question fragments and a weight value for the selected fragment; the acquisition module is further configured to: acquire a first code fragment from at least one private domain code fragment based on the second question; wherein the private domain code fragment is a code fragment with preset annotation prompts; the weight value of the first code fragment matches that of the selected fragment; and a determination module configured to: use the first code fragment and the first question as input to a question-answering model, and use the question-answering model to obtain target code.

[0023] Based on this solution, by analyzing the user's initial question using pre-defined segments, the weight values ​​of key question segments are updated. Furthermore, based on the updated initial question, the system accurately identifies the first code segment most relevant to the user's query and containing labeled prompts. The model then generates the target code. This ensures that the generated target code better matches the user's preferences, thereby improving the overall user experience.

[0024] In one possible implementation, the matching module is specifically configured to: determine at least one question segment in the first question based on a segmentation rule; the segmentation rule is used to characterize the segmentation based on at least one of the structure, semantics, and logical relationships of the user-input question; determine at least one selected segment in the at least one question segment based on a preset segment; adjust the weight value of each selected segment; and determine the weight value of the question segment based on the weight values ​​of the non-selected segment and the selected segment; wherein the non-selected segment is a question segment that does not match the preset segment.

[0025] In one possible implementation, the acquisition module is specifically configured to: acquire the first code fragment corresponding to the second question based on the similarity between the second question and each private domain code fragment.

[0026] In one possible implementation, the acquisition module is further specifically configured to: acquire the target feature vector corresponding to the second problem; calculate the similarity between the target feature vector and each first feature vector; the first feature vector is used to characterize the features of the private domain code fragment; and acquire the first code fragment corresponding to the second problem based on at least one similarity.

[0027] In one possible implementation, a build module is also included, which is further configured to: build a private codebase; the private codebase stores at least one private code fragment with preset tag prompts.

[0028] In one possible implementation, the building module is further configured to: parse the private domain code file based on syntax nodes to obtain at least one code segment corresponding to the private domain code file; process each code segment based on a preset annotation method and preset segments to obtain at least one code segment with preset annotation prompts; and store the at least one code segment with preset annotation prompts in the private domain code library.

[0029] Thirdly, embodiments of this application also provide a computing device, including: a processor and a memory; the processor and the memory are coupled; the memory is used to store program instructions; the processor is used to execute the program instructions to perform the method as described in any of the first aspects above.

[0030] Fourthly, embodiments of this application provide a chip for performing the methods described in any of the first aspects above.

[0031] Fifthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a computer, implement the method as described in any of the first aspects.

[0032] In a sixth aspect, embodiments of this application provide a program product including a computer program that, when executed by a processor, implements the method as described in any of the first aspects. Attached Figure Description

[0033] Figure 1 This is a first flowchart illustrating a code generation method provided in an embodiment of this application;

[0034] Figure 2 This is a schematic diagram of a code generation framework provided in an embodiment of this application;

[0035] Figure 3 This is a schematic diagram of a process for determining the weight value of a problem segment according to an embodiment of this application;

[0036] Figure 4This is a schematic diagram of a process for obtaining a first code fragment provided in an embodiment of this application;

[0037] Figure 5 This is a schematic diagram of a process for building a private domain code library provided in an embodiment of this application;

[0038] Figure 6 This is a schematic diagram of a framework for building a private domain code library provided in an embodiment of this application;

[0039] Figure 7 This is a schematic diagram of a process for obtaining code fragments provided in an embodiment of this application;

[0040] Figure 8 This is a second flowchart illustrating code generation provided in an embodiment of this application;

[0041] Figure 9 This is a schematic diagram of a code generation device provided in an embodiment of this application;

[0042] Figure 10 This is a schematic diagram of a computing device provided in an embodiment of this application. Detailed Implementation

[0043] The technical solutions of the embodiments of this application will now be described with reference to the accompanying drawings. To facilitate a clear description of the technical solutions of the embodiments of this application, the use of terms such as "first," "second," etc., in the embodiments of this application is for illustrative purposes and to distinguish the objects being described. There is no particular order between them, nor does it indicate a specific limitation on the number of devices in the embodiments of this application, and they do not constitute any limitation on the embodiments of this application.

[0044] To enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in 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, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of this application.

[0045] It should be noted that many specific details are set forth in the following description in order to provide a full understanding of this application. However, this application may also be implemented in other ways different from those described herein. Therefore, the scope of protection of this application is not limited to the specific embodiments disclosed below.

[0046] In the description of this application, it should be understood that the terms "upper," "lower," "horizontal," "bottom," "inner," and "outer" (if any) indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. These terms are used only for the convenience of describing this application and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this application. In this application, unless otherwise expressly specified and limited, "upper" or "lower" of the first feature and the second feature may mean that the first and second features are in direct contact, or that the first and second features are in indirect contact through an intermediate medium.

[0047] In this application, unless otherwise expressly specified and limited, the terms "connected," "linked," and "fixed," etc., should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral unit; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two components or the interaction between two components. However, specifying a direct connection indicates that the two entities at the point of connection are not connected through a transitional structure, but are simply linked together to form a whole. For those skilled in the art, the specific meaning of the above terms in this application can be understood according to the specific circumstances.

[0048] In this application, the use of terms such as "first," "second," etc., is for descriptive purposes only and should not be construed as indicating or implying their relative importance or implicitly specifying the number of technical features indicated. Therefore, a feature defined with "first" or "second" may explicitly or implicitly include at least one of those features.

[0049] The following explanations of the technical terms mentioned in the embodiments of this application are provided to facilitate understanding by those skilled in the art.

[0050] Retrieval-augmented generation (RAG) technology is a system architecture that combines information retrieval and text generation techniques. It aims to generate answers or content by referencing information from external knowledge bases. In other words, it generates more accurate answers or content by referencing information from external knowledge bases. The framework mainly consists of two parts: a retrieval module and a generation module. The retrieval module is responsible for accurately finding the most relevant information fragments to the user's query from a large number of documents or data sources; while the generation module uses this retrieved relevant information to generate high-quality, targeted answers or content through a question-answering model.

[0051] Private domain code documentation is code documentation used exclusively within an organization or by an individual. It is typically subject to strict access controls to ensure its security and confidentiality, is not publicly disclosed, and can only be accessed by authorized personnel.

[0052] Embedding is a technique that maps discrete symbolic data (such as words, images, users, etc.) to a low-dimensional continuous vector space, transforming complex symbolic information into numerical vectors that can capture the semantic or structural relationships between data.

[0053] The "problem" in the embodiments of this application may also be referred to as an instruction, prompt, or alert.

[0054] The embodiments of this application will now be described with reference to the accompanying drawings.

[0055] Figure 1 This is a first flowchart illustrating a code generation method provided in an embodiment of this application.

[0056] Figure 2 This is a schematic diagram of a code generation framework provided in an embodiment of this application.

[0057] like Figure 1 and Figure 2 As shown, the code generation method includes the following steps:

[0058] Step S1: Obtain the first question input by the user.

[0059] In one implementation, a user inputs a first question into the code development software of a computing device, and the computing device receives the user's input. This first question is a description representing the user's needs. Its format is flexible and can include both descriptive text and code, or only descriptive text, or only code. In other words, users can choose the most suitable way to ask questions based on their questioning habits or expression preferences. For example, for functional requirements, a user could ask, "Write a function to achieve…"; for requirements regarding processing steps, a user could ask, "Write a script to first process…, then implement…, and finally…"; and for code requiring comments, a user could ask, "Write a paragraph explaining the processing steps and logic of the code, the code content is as follows:…", etc.

[0060] For example, regarding functional requirements, a user can enter the following first question into the code development software of a computing device, which includes both descriptive text and code. For instance: "A simple logging function works well on platform A, and now we want to migrate this functionality to platform B. Based on the code snippet provided below for platform A '…', please generate an equivalent logging implementation for platform B, and point out the main differences and areas for attention."

[0061] It should be noted that a problem can be broken down into multiple problem segments, each focusing on a specific aspect or element of the problem. Some problem segments in a problem may be more critical than others. To facilitate subsequent retrieval and processing, these important problem segments need to be specially marked. This can be achieved by matching them with pre-defined preset segments, as explained in step S2 below.

[0062] Step S2: Based on the matching results of at least one question fragment contained in the first question with the preset fragment, determine the weight value of each question fragment.

[0063] In one implementation, combining Figure 3 As shown, the weight value for determining each problem segment is explained.

[0064] Figure 3 This is a schematic diagram of a process for determining the weight value of a problem segment according to an embodiment of this application.

[0065] like Figure 3 As shown, determining the weight value of a problem segment can include the following steps:

[0066] Step S21: Based on the partitioning rules, determine at least one problem segment in the first problem.

[0067] A question fragment refers to multiple components obtained by breaking down a complete question. Each component focuses on a specific aspect or element of the question. By breaking the question down into fragments, the user's intent can be more clearly identified and the code logic analyzed. In the embodiments of this application, question fragments may also be referred to as fields, tokens, tokens, etc., without specific limitations.

[0068] The partitioning rule is used to characterize the partitioning based on at least one of the structural, semantic, and logical relationships of the user input question. The partitioning rule can be preset and is not specifically limited here.

[0069] Continuing with the example above, based on the segmentation rules, the first question, "A simple logging function works well on platform A, and now we want to migrate this functionality to platform B. Based on the code snippet provided below for platform A, please generate an equivalent code snippet for platform B, and point out the main differences and points to note," is segmented into multiple question snippets, such as "logging function," "platform A," "platform B," and "code snippet." These question snippets from the first question will not be listed individually here.

[0070] Step S22: Based on preset segments, determine at least one selected segment from at least one problem segment.

[0071] Among them, the preset fragments are key fragments that are specified in advance, such as "platform name" and "interface name", and are not specifically limited here.

[0072] In one example, at least one selected segment from at least one question segment can be determined based on the similarity between a preset segment and a question segment. For example, vector features of the preset segment and the question segment are extracted respectively using a pre-trained embedding model, the distance between the vector features of the preset segment and the question segment is calculated, and then the similarity between the preset segment and the question segment is determined. If the similarity is greater than a preset threshold, at least one selected segment from at least one question segment is determined.

[0073] The selected segment may include a first target segment and / or a second target segment. The first target segment is a problem segment that completely matches a preset segment, and the second target segment is a problem segment that partially matches a preset segment. The first target segment has a higher importance than the second target segment. This importance can be represented by a weight value; for example, the first target segment has a higher weight value than the second target segment.

[0074] Continuing with the example above, if the preset fragment is "Platform Name", based on the similarity between the preset fragment and each question fragment, the selected fragments in the first question, "A simple logging function works well on platform A, and now we want to migrate this function to platform B. Based on the code snippet for platform A provided below, please generate an equivalent logging implementation code snippet for platform B, and point out the main differences and points to note," are "Platform A", "Platform B", "Code Snippet for Platform A", and "Code Snippet for Platform B". Among these, "Platform A" and "Platform B" are the first target fragments that completely match the preset fragment, and "Code Snippet for Platform A" and "Code Snippet for Platform B" are the second target fragments that partially match the preset fragment.

[0075] Step S23: Adjust the weight value of each selected segment to obtain the weight value of the problem segment.

[0076] There are various ways to obtain the weight value of a question fragment. For example, you can determine the weight value of a question fragment by repeatedly listing it, or by labeling it. The following will illustrate this with specific examples.

[0077] In one example, the weight of a problem fragment is determined by repeatedly listing it, that is, by counting the frequency of its appearance in the problem description.

[0078] Continuing with the example above, in the first question, "Platform A" appeared twice, "Platform B" also appeared twice, and "Code snippet for Platform A" and "Code snippet for Platform B" each appeared once. Based on this, repeat "Platform A" and "Platform B" twice, and repeat "Code snippet for Platform A" and "Code snippet for Platform B" once.

[0079] In another example, the weight of the problem segment is determined by using a labeling method.

[0080] Continuing with the example above, in the first question, "Platform A" appears twice, "Platform B" also appears twice, and "Code snippet for Platform A" and "Code snippet for Platform B" each appear once. Based on this, the weight values ​​of "Platform A" and "Platform B" are adjusted, for example, their corresponding weight values ​​are 3 each. The weight values ​​of "Code snippet for Platform A" and "Code snippet for Platform B" are adjusted to 2. The default weight value for other non-selected snippets is 1. After adjusting the weight values ​​of each question snippet in the first question, the weight values ​​of the question snippets are obtained.

[0081] Step S3: Generate the second question.

[0082] The second question includes at least one selected segment from the question segments and a weight value for the selected segment.

[0083] Following the example of tagging above, the second generated question is: "A simple logging function works well on platform A, and we want to migrate this functionality to platform B. Based on the code snippet provided below for platform A, please generate an equivalent code snippet for platform B, and point out the main differences and areas for attention." The weight values ​​of the question snippets are indicated by superscript numbers.

[0084] Following the example of repetitive listing above, the second generated question is: "A simple logging function works well on platform A, and we want to migrate this functionality to platform B. Based on the code snippet provided below for platform A, please generate an equivalent code snippet for platform B, and point out the main differences and areas for attention. Platform A, Platform A, Platform B, Platform B, Code snippet for Platform A, Code snippet for Platform B." Label the weight values ​​of the problem snippets in the form of reproducible problem snippets.

[0085] Step S4: Based on the second question, obtain the first code snippet from at least one private domain code snippet.

[0086] Among them, at least one private domain code snippet is pre-stored in the private domain code database. The private domain code snippet is a code snippet with a preset annotation prompt word. The preset annotation prompt word is determined based on the annotation method and the preset snippet. The annotation method is a method specified by the user according to their questioning habits. The following combines Figure 4 , based on the second question, to obtain the first code snippet from at least one private domain code snippet in the private domain code library for example.

[0087] Figure 4 is a schematic flowchart of a process for obtaining the first code snippet provided by an embodiment of the present application.

[0088] In one implementation, as Figure 4 shown, step S4 includes steps S41 - S42.

[0089] Step S41: Obtain at least one private domain code snippet from the private domain code library.

[0090] Continuing with the above example, obtain multiple private domain code snippets from the private domain code library. Taking four private domain code snippets as an example, such as private domain code snippet a, private domain code snippet b, private domain code snippet c, and private domain code snippet d.

[0091] Step S42: Obtain the similarity between the second question and each private domain code snippet.

[0092] Continuing to combine Figure 4 shown, step S42 includes steps S421 - step S423.

[0093] Step S421: Obtain the target feature vector corresponding to the second question.

[0094] There are various ways to extract the target feature vector of the second question. For example, the target feature vector of the second question can be extracted using a pre-trained model, or the syntactic structure of the second question can be analyzed using natural language processing techniques to identify the components in the question, and the target feature vector of the second question can be extracted based on these components.

[0095] The following takes extracting the target feature vector of the second question using a pre-trained model as an example for illustration.

[0096] First, preprocess the second question, and screen the question fragments in the second question, such as excluding some stop words and meaningless words. Stop words refer to question fragments that appear frequently in the second question but contribute little to the semantics, such as "of", "is", etc.

[0097] Second, use an embedding model to obtain the fragment vector of each question fragment in the second question.

[0098] An embedding model is a machine learning model that has been trained on a large-scale dataset before use. This embedding model is primarily used to extract feature vectors. Furthermore, the embedding model needs to be trained before it can be used to extract the fragment vector for each problem segment.

[0099] In one example, a training set is obtained, comprising at least one sample question including sample question fragments and a target fragment vector corresponding to each sample question fragment. Next, for the more important question fragments in each sample question, data augmentation techniques are used to generate more sample question fragments and sample questions, resulting in an updated training set. Further, each sample question including a sample question fragment in the updated training set is used as input to an embedding model, and the target fragment vector corresponding to each sample question fragment is used as the output of the embedding model to train the embedding model. After the embedding model is trained, it is used to obtain the fragment vector of each question fragment in the second question.

[0100] Finally, the average value of each segment vector is calculated to obtain the target feature vector corresponding to the second problem.

[0101] Step S422: Calculate the similarity between the target feature vector and each first feature vector.

[0102] The first feature vector is used to characterize the features of the private domain code fragment.

[0103] In one example, step S422 includes steps S4221-S4222.

[0104] Step S4221: Obtain the first feature vector corresponding to each private domain code fragment in the private domain code database.

[0105] Step S4221 can refer to step S421 above, and is not specifically limited here.

[0106] Continuing with the example above, let's take four private domain code fragments as examples, such as the first feature vector a' corresponding to private domain code fragment a, the first feature vector b' corresponding to private domain code fragment b, the first feature vector c' corresponding to private domain code fragment c, and the first feature vector d' corresponding to private domain code fragment d.

[0107] Step S4222: Calculate the similarity between the target feature vector and each first feature vector.

[0108] Continuing with the example above, calculate the similarity Ma between the target feature vector and the first feature vector a', for example, Ma is 0.8; calculate the similarity Mb between the target feature vector and the first feature vector b', for example, Mb is 0.6; calculate the similarity Mc between the target feature vector and the first feature vector c', for example, Mc is 0.3; calculate the similarity Md between the target feature vector and the first feature vector d', for example, Md is 0.7.

[0109] Step S43: Based on the similarity between the second question and each private domain code fragment, obtain the first code fragment corresponding to the second question.

[0110] The first code snippet is matched with the weight value of the selected snippet.

[0111] In one example, there are various ways to obtain the first code snippet based on similarity. For example, the first code snippet can be determined by sorting the similarity and selecting the maximum value, or by using a similarity threshold to filter out private domain code snippets that meet the similarity threshold, and then selecting the first code snippet that best meets the requirements from these private domain code snippets. The following is an example to illustrate one of these methods.

[0112] Step S43 includes steps S431-S432.

[0113] Step S431: Obtain the target similarity among multiple similarity scores.

[0114] Among them, the target similarity is the highest similarity value calculated from the similarity between the second question and each private domain code fragment.

[0115] Continuing with the example above, the target similarity is determined from the four similarities mentioned above as: the similarity Ma between the target feature vector and the first feature vector a'.

[0116] Step S432: Use the private domain code fragment corresponding to the target similarity as the first code fragment corresponding to the second question.

[0117] Continuing with the example above, based on the similarity Ma between the target feature vector and the first feature vector a', the private domain code fragment a is determined to be the first code fragment N. Here, the first code fragment is a code fragment with preset labeled prompts.

[0118] Step S5: Using the first code snippet and the first question as input to the question-answering model, the target code is obtained using the question-answering model.

[0119] Continuing with the example above, using the first code snippet N and the first question, "A simple logging function works well on platform A, and now we want to migrate this functionality to platform B. Based on the code snippet provided below for platform A, please generate an equivalent logging implementation for platform B, and point out the main differences and points to note," as input to the question-answering model, the target code is obtained using the question-answering model.

[0120] It should be noted that if the first question only includes code, the default description text will be "Describe the function and processing steps of the code in a paragraph. Do not fabricate content or provide unnecessary information. The code content is as follows:...".

[0121] In summary, this embodiment of the application analyzes the user's first question by pre-setting a segment, updates the weight values ​​of the key question segments within it, and further, based on the updated first question, accurately finds the first code segment most relevant to the user's first question and containing labeled prompt words. The model is then used to generate target code. This ensures that the generated target code better matches the user's preferences, thereby improving the overall user experience.

[0122] Corresponding to the above embodiments, this application also provides a method for constructing a private domain codebase, which is described below in conjunction with... Figure 5 and Figure 6 An example is provided.

[0123] Figure 5 This is a schematic diagram of a process for building a private domain code library provided in an embodiment of this application.

[0124] Figure 6 This is a schematic diagram of a framework for building a private domain code library provided in an embodiment of this application.

[0125] like Figure 5 and Figure 6 As shown, the method for building a private domain codebase includes the following steps:

[0126] Step S01: Build a private domain codebase.

[0127] The private domain code library stores at least one private domain code fragment with preset annotation prompts.

[0128] In one implementation, step S01 includes steps S001-S003.

[0129] Step S001: Based on the syntax nodes, parse the private domain code file to obtain at least one code fragment corresponding to the private domain code file.

[0130] Private domain code files come in various types due to different programming languages, such as .py files for Python, .c files for C, .cpp / .hpp files for C++, and .sh files for shell scripting. No specific limitation is made here.

[0131] The following explanation uses a Python-language private domain code file as an example.

[0132] In one implementation, step S001 includes steps S010-S011.

[0133] Step S010: Parse the private domain code file to obtain the code syntax structure.

[0134] The code syntax structure can be represented using an abstract syntax tree.

[0135] In one example, the private domain code file is converted into a code string and preprocessed, such as by removing comments. Then, an appropriate parser is selected based on the programming language used in the code file; for example, the `ast` module can be used to parse private domain code files in Python. By calling the `ast` module to parse the code string, an abstract syntax tree representing the code's syntactic structure is generated. Each node in the tree structure corresponds to a syntactic structure in the private domain code file, such as an expression or statement.

[0136] Step S011: Based on the syntax nodes, the code syntax structure is segmented to obtain at least one code fragment.

[0137] A syntax node is one of the fundamental elements that make up an abstract syntax tree (AST). An AST is a tree-like structure used to represent the structured representation of source code. Each node represents a syntax structure in the source code, such as an expression, statement, function definition, or class declaration. Specifically, syntax nodes include at least one of the following: functions, classes, structs, and interfaces.

[0138] For example, the code syntax structure is segmented based on at least one of functions, classes, structures, and interfaces to obtain at least one code fragment, such as code fragment a1, code fragment b1, code fragment c1, and code fragment d1.

[0139] Step S002: Based on the preset annotation method and preset segments, process each code segment to obtain at least one code segment with preset annotation prompts.

[0140] Figure 7This is a flowchart illustrating a method for obtaining code fragments, as provided in an embodiment of this application.

[0141] In one example, such as Figure 7 As shown, step S002 includes steps S020-S022.

[0142] Step S020: Obtain the annotation method created by the user.

[0143] Here, annotation style refers to the annotation style customized by the user based on their personal questioning habits and programming habits, clearly expressing the annotation content the user expects. For example, obtaining user-created annotation styles such as "annotation style for functional descriptions", "annotation style for descriptions of processing steps", and "annotation style for code that needs comments" are examples.

[0144] Step S021: Obtain the preset fragment created by the user.

[0145] Among them, the preset fragments are key fragments that are specified in advance, such as "platform name" and "interface name".

[0146] Step S022: Based on the preset annotation method and preset segments, process each code segment to obtain at least one code segment with preset annotation prompts.

[0147] There are various ways to obtain at least one code snippet with preset tag prompts. It can be done manually or by using a model, such as a question-and-answer model. The following example illustrates how to obtain at least one code snippet with preset tag prompts by using a model.

[0148] For example, using a preset annotation method, a preset fragment, and at least one code fragment as input to a question-answering model, the model obtains at least one code fragment with preset annotation prompts. For instance, using a preset annotation method, a preset fragment, and code fragments a1, b1, c1, and d1 as input to a question-answering model, the model obtains private domain code fragments a, b, c, and d.

[0149] Step S003: Store at least one code snippet with preset tag prompts into the private domain code repository.

[0150] Following the example above, private domain code snippets a, b, c, and d are stored in the private domain code repository.

[0151] In summary, by building a private domain code library, we can accurately locate the information fragments that best match the user's query, thus providing a more accurate reference for the subsequent generation of target code for the question-answering model.

[0152] Corresponding to the above embodiments, this application also provides another specific embodiment of code generation, which will be described below in conjunction with... Figure 8 An example is provided.

[0153] Figure 8 This is a second flowchart illustrating a code generation process provided in an embodiment of this application.

[0154] like Figure 8 As shown, the code generation method includes the following steps:

[0155] Step S801: Obtain the private domain code file.

[0156] Step S802: Perform syntax parsing on the private domain code file and convert it into an abstract syntax tree.

[0157] Step S803: Segment multiple nodes of the abstract syntax tree based on syntax nodes to obtain multiple code snippets.

[0158] Step S804: Obtain the annotation method created by the user.

[0159] Step S805: Obtain the preset fragment created by the user.

[0160] Step S806: Process multiple fragments based on the annotation method and preset fragments to obtain multiple private domain code fragments.

[0161] Step S807: Store multiple private domain code snippets in the private domain code repository.

[0162] Step S808: Obtain the first question input by the user.

[0163] Step S809: Adjust the weight values ​​of the problem segments in the first problem to generate the second problem.

[0164] Step S810: Based on the second question, retrieve the corresponding first code snippet from the private domain code repository.

[0165] Step S811: Input the first problem and the first code snippet into the problem model to obtain the target code.

[0166] In summary, this embodiment of the application analyzes the user's first question by pre-setting a segment, updates the weight values ​​of the key question segments within it, and further, based on the updated first question, accurately finds the first code segment most relevant to the user's first question and containing labeled prompt words. The model is then used to generate target code. This ensures that the generated target code better matches the user's preferences, thereby improving the overall user experience.

[0167] Corresponding to the embodiments of the aforementioned code generation method, this application also provides embodiments of a code generation apparatus.

[0168] Figure 9 This is a schematic diagram of a code generation device provided in an embodiment of this application.

[0169] like Figure 9 As shown, the code generation device 900 includes: an acquisition module 901, a matching module 902, a generation module 903, a determination module 904, and a construction module 905.

[0170] The acquisition module 901 is configured to: acquire a first question input by the user; the first question is a description of user needs; the matching module 902 is configured to: determine the weight value of each question segment based on the matching result of at least one question segment contained in the first question with a preset segment; the generation module 903 is configured to: generate a second question; wherein the second question includes at least one selected segment selected from the question segments and the weight value of the selected segment; the acquisition module 901 is further configured to: acquire a first code segment from at least one private domain code segment based on the second question; wherein the private domain code segment is a code segment with preset labeled prompt words; the weight value of the first code segment matches that of the selected segment; the determination module 904 is configured to: use the first code segment and the first question as input to a question-answering model, and use the question-answering model to obtain the target code.

[0171] In one possible implementation, the matching module 902 is specifically configured to: determine at least one question segment in the first question based on a segmentation rule; the segmentation rule is used to characterize the segmentation based on at least one of the structure, semantics, and logical relationships of the user input question; determine at least one selected segment in the at least one question segment based on a preset segment; adjust the weight value of each selected segment; and determine the weight value of the question segment based on the weight values ​​of the non-selected segment and the selected segment; wherein the non-selected segment is a question segment that does not match the preset segment.

[0172] In one possible implementation, the acquisition module 901 is specifically configured to: acquire the first code segment corresponding to the second question based on the similarity between the second question and each private domain code segment.

[0173] In one possible implementation, the acquisition module 901 is further configured to: acquire the target feature vector corresponding to the second problem; calculate the similarity between the target feature vector and each first feature vector; the first feature vector is used to characterize the features of the private domain code fragment; and acquire the first code fragment corresponding to the second problem based on at least one similarity.

[0174] In one possible implementation, the building module 905 is further configured to: build a private codebase; the private codebase stores at least one private code fragment with preset tag prompts.

[0175] In one possible implementation, the construction module 905 is further configured to: parse the private domain code file based on syntax nodes to obtain at least one code segment corresponding to the private domain code file; process each code segment based on a preset annotation method and preset segments to obtain at least one code segment with preset annotation prompts; and store the at least one code segment with preset annotation prompts in the private domain code library.

[0176] The principles and beneficial effects of the various modules and units of the reasoning task execution device provided in this application embodiment can be found in the aforementioned reasoning task execution method embodiment, and will not be repeated here.

[0177] This application also provides a computing device, including: a processor and a memory; the processor and the memory are coupled; the memory is used to store program instructions; the processor is used to execute the program instructions to perform a method as described in any of the above embodiments.

[0178] In some embodiments, the computing device may include devices such as servers, terminals, rack servers, and blade servers; the computing device includes one or more processors and memory. The memory is configured to store one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the code generation and execution method described in the above embodiments.

[0179] Figure 10 This is a schematic diagram of a computing device provided in an embodiment of this application.

[0180] like Figure 10 As shown, the computing device 1000 includes a processor 1001 and a memory 1002. Exemplarily, the computing device 1000 may also include a communications interface 1003 and a communications bus 1004.

[0181] The processor 1001, memory 1002, and communication interface 1003 communicate with each other via communication bus 1004. The communication interface 1703 may include a transmitter and receiver for communicating with other devices or communication networks, and may be a wired interface (port), such as a fiber distributed data interface (FDDI) or a gigabit Ethernet interface (GE).

[0182] In some embodiments, the processor 1001 is used to execute the program 1005, specifically performing the relevant steps in the above-described inference task execution method embodiments. Specifically, the program 1005 may include program code, which includes computer-executable instructions.

[0183] For example, processor 1001 may be a central processing unit (CPU), an application-specific integrated circuit (ASIC), or one or more integrated circuits configured to implement some embodiments of this application. Computing device 1000 may include one or more processors, which may be processors of the same type, such as one or more CPUs; or they may be processors of different types, such as one or more CPUs and one or more ASICs. The CPU may be a single-core CPU or a multi-core CPU.

[0184] In some embodiments, memory 1002 is used to store program 1005. Memory 1002 may include high-speed random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage device.

[0185] Specifically, program 1005 can be called by processor 1001 to cause computing device 1000 to perform inference task execution method operations.

[0186] Some embodiments of this application provide a computer-readable storage medium storing at least one executable instruction that, when executed on a computing device 1000, causes the computing device 1000 to perform the code generation and execution method described above.

[0187] The executable instructions can be used to enable the computing device 1000 to perform inference task execution methods.

[0188] For example, the computer-readable storage medium can be a read-only memory (ROM), a random access memory (RAM), a compact disc read-only memory (CD-ROM), magnetic tape, a floppy disk, and an optical data storage device.

[0189] Some embodiments of this application provide a chip system applied to a server. The chip system includes one or more interface circuits and one or more processors. The interface circuits and processors are interconnected via lines. The interface circuits are used to receive signals from the server's memory and send signals to the processors, the signals including computer instructions stored in the memory. When the server processor executes the computer instructions, the server performs various steps in the inference task execution method shown in the above-described method embodiments.

[0190] The beneficial effects that the readable storage medium provided in some embodiments of this application can achieve can be referred to the beneficial effects in the corresponding inference task execution method provided above, and will not be repeated here.

[0191] It should be noted that, in this application, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes the element.

[0192] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the apparatus embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.

[0193] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by, or in conjunction with, an instruction execution system, apparatus or device (such as a computer-based system, a processor-included system or other system that can fetch and execute instructions from, an instruction execution system, apparatus or device).

[0194] For the purposes of this specification, "computer-readable medium" can mean any means that can contain, store, communicate, propagate, or transmit programs for use by or in conjunction with an instruction execution system, apparatus, or device.

[0195] More specific examples (a non-exhaustive list) of computer-readable media include the following: electrical connections having one or more wires (electronic devices), portable computer disks (magnetic devices), random access memory (RAM), read-only memory (ROM), erasable and editable read-only memory (EPROM or flash memory), fiber optic devices, and portable optical disc read-only memory (CDROM).

[0196] Furthermore, the computer-readable medium can even be paper or other suitable media on which the program can be printed, because the program can be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, interpreting, or otherwise processing as necessary, and then stored in computer memory. It should be understood that various parts of this application can be implemented using hardware, software, firmware, or a combination thereof.

[0197] In the above embodiments, multiple steps or methods can be implemented using software or firmware stored in memory and executed by a suitable instruction execution system. For example, if implemented in hardware, as in another embodiment, it can be implemented using any one or a combination of the following techniques known in the art: discrete logic circuits having logic gates for implementing logical functions on data signals, application-specific integrated circuits (ASICs) having suitable combinational logic gates, programmable gate arrays (PGAs), field-programmable gate arrays (FPGAs), etc. The embodiments described above are merely specific embodiments of this application and are not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, improvements, etc., made based on the technical solutions of this application should be included within the scope of protection of this application.

Claims

1. A code generation method, characterized in that, include: The first question in obtaining user input; The first problem is the descriptive content used to characterize user needs; Based on the matching results of at least one question segment contained in the first question with a preset segment, a weight value for each question segment is determined; Generate a second question; wherein the second question includes at least one selected segment from the question segments and a weight value of the selected segment; Based on the second problem, a first code segment is obtained from at least one private domain code segment; wherein, the private domain code segment is a code segment with preset annotation prompt words; the weight value of the first code segment is matched with that of the selected segment; Using the first code snippet and the first question as input to the question-answering model, the target code is obtained using the question-answering model.

2. The code generation method according to claim 1, characterized in that, The step of determining the weight value of the question segment based on the matching result between the question segment contained in the first question and the preset segment includes: Based on the partitioning rules, at least one question segment in the first question is determined; the partitioning rules are used to characterize the partitioning based on at least one of the structure, semantics, and logical relationships of the user input question; Based on the preset segment, at least one selected segment from the at least one problem segment is determined; Adjust the weight value of each selected segment to obtain the weight value of the problem segment.

3. The code generation method according to claim 1 or 2, characterized in that, The selected segment includes at least one first target segment and / or at least one second target segment, wherein the first target segment is a problem segment that matches the preset segment, and the second target segment is a problem segment that partially matches the preset segment; the weight value of the first target segment is higher than the weight value of the second target segment.

4. The code generation method according to any one of claims 1-3, characterized in that, Its features are, The step of obtaining the first code fragment from at least one private domain code fragment based on the second problem includes: Based on the similarity between the second question and each of the private domain code fragments, the first code fragment corresponding to the second question is obtained.

5. The code generation method according to claim 4, characterized in that, The step of obtaining the first code segment corresponding to the second question based on the similarity between the second question and each of the private domain code segments includes: Obtain the target feature vector corresponding to the second problem; Calculate the similarity between the target feature vector and each first feature vector; the first feature vector is used to characterize the features of the private domain code fragment. Based on the at least one similarity, obtain the first code segment corresponding to the second question.

6. The code generation method according to any one of claims 1-5, characterized in that, The first problem includes at least one of descriptive text and code.

7. The code generation method according to any one of claims 1-6, characterized in that, Before retrieving the first code segment from at least one private domain code segment based on the second problem, the method further includes: Construct a private domain code library; the private domain code library stores at least one private domain code fragment with preset annotation prompts.

8. The code generation method according to claim 7, characterized in that, The construction of the private domain code database includes: Based on syntax nodes, the private domain code file is parsed to obtain at least one code fragment corresponding to the private domain code file; Based on a preset annotation method and preset segments, each code segment is processed to obtain at least one code segment with preset annotation prompts; Store at least one of the code snippets with preset annotation prompts into the private domain code library.

9. A code generation device, characterized in that, The device includes: The acquisition module is configured to: acquire a first question input by the user; the first question is a description of the user's needs; The matching module is configured to: determine the weight value of each of the question segments based on the matching results of at least one question segment contained in the first question with a preset segment; The generation module is configured to: generate a second question; wherein the second question includes at least one selected segment from the question segments and a weight value of the selected segment; The acquisition module is further configured to: based on the second question, acquire a first code segment from at least one private domain code segment; wherein the private domain code segment is a code segment with preset annotation prompt words; and the weight value of the first code segment matches that of the selected segment. The determination module is configured to: take the first code snippet and the first question as input to the question-answering model, and use the question-answering model to obtain the target code.

10. A computing device, characterized in that, The computing device includes a memory and a processor; the memory and the processor are coupled; the memory is used to store computer program code, the computer program code including computer instructions, which, when executed by the processor, cause the computing device to perform the code generation method as described in any one of claims 1 to 8.