A task-oriented code segment large model prompt word optimization method and system
By extracting similar code examples from the codebase and constructing multi-dimensional prompt words, the problem of insufficient understanding of complex task code segments by large models is solved, and more accurate code generation and understanding are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HEFEI UNIV OF TECH
- Filing Date
- 2026-02-05
- Publication Date
- 2026-06-05
AI Technical Summary
Existing large models lack the ability to understand complex task code segments and lack specific knowledge, resulting in deviations between generated results and user needs.
The BM25 algorithm is used to extract the code samples with the highest similarity from the code base, perform embedding vector similarity calculation, construct the first part of the prompt words based on the repository, path and function name information, construct the second and third parts of the prompt words based on the data flow graph and syntax tree, and integrate them into the complete prompt words input large model.
It improves the ability of large models to understand task code segments, generates more accurate code results, captures the semantic and structural information of the code, and enhances the adaptability and performance of the model.
Smart Images

Figure CN122152359A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, specifically to a method and system for optimizing large model prompt words for task-oriented code segments. Background Technology
[0002] With the rapid development of artificial intelligence technology, large models have demonstrated powerful application capabilities in numerous fields, especially in the area of task code segments. Whether it's code generation, code understanding, code optimization, or answering code-related questions, large models are gradually becoming important tools. However, when faced with complex task code segments, how to more effectively guide large models to complete the task code segments, enabling them to more accurately understand task requirements, generate code more precisely, or improve code-related solutions, has become an urgent problem to be solved.
[0003] When processing task code segments, existing large models often encounter biases in understanding the semantics, structure, and specific requirements of the task due to the highly structured and logical nature of these segments. Furthermore, task code segments frequently involve specific programming languages, frameworks, and tools, while existing large models lack the ability to understand these specific knowledge bases. Directly inputting the original code or a simple task description can lead to discrepancies between the model's output and the user's needs.
[0004] Therefore, addressing the shortcomings of large models in understanding code segments for complex tasks has become an urgent problem to be solved. Summary of the Invention
[0005] (a) Technical problems to be solved To address the shortcomings of existing technologies, this invention provides a method and system for optimizing prompt words for large models oriented towards task code segments, solving the problem of insufficient understanding ability of traditional large models when processing complex task code segments.
[0006] (II) Technical Solution To achieve the above objectives, the present invention provides the following technical solution: In a first aspect, the present invention provides a method for optimizing large model prompt words for task code segments, the method comprising: Obtain the task code segment input by the user, and obtain the corresponding code library based on the task code segment; Based on the BM25 algorithm, n code segments with the closest similarity to the task code segment are extracted from the code library as the first code sample group. The embedding vector similarity of the first code sample group is calculated and filtered to obtain the second code sample group; the second code sample group includes multiple highly similar code samples and one least similar code sample; Extract the repository, path, and function name information from the second code sample group and the task code segment to construct the first part of the prompt words; construct and parse a data flow graph based on the second code sample group and the task code segment to construct the second part of the prompt words; construct a syntax tree based on the second code sample group and the task code segment and perform hierarchical analysis to construct the third part of the prompt words; After integrating the first part of the prompt words, the second part of the prompt words, and the third part of the prompt words into a complete prompt word, the input is given to the large model to obtain the output result corresponding to the task code segment.
[0007] Preferably, after obtaining the task code segment input by the user and obtaining the corresponding code library based on the task code segment, the method further includes: Select the corresponding parser and code library based on the programming language type of the task code segment; The task code segment and the code library are segmented by the parser to obtain a code token.
[0008] Preferably, the step of constructing and parsing a data flow graph based on the second code sample group and the task code segment to construct the second part of the prompt words includes: Convert the second code sample group and the task code segment into a code attribute graph; Extract the data flow graph from the code attribute graph; The data flow graph is traversed and filtered to obtain data flow edge information; The data flow edge information is organized as follows: A list of data flow edges for the structure; where, srcName It is the starting node of the data stream. from It is the starting node's number in the token sequence. to It is a list of token sequence numbers of the end nodes of all data flow edges originating from the same starting node; The data stream side list is processed into a second part of prompt words using a preset format.
[0009] Preferably, the step of constructing a syntax tree and performing hierarchical analysis based on the second code sample group and the task code segment to construct the third part of the prompt words includes: The second code sample group and the task code segment are parsed using an abstract syntax tree to obtain variable definition points; the variable definition points include variable declarations, assignments, and return functions. Extract the node content corresponding to the type of the task code segment from the variable definition point to obtain a node list; the node list includes a function parameter list, a return value list, a method call list, and a method parameter list; The node list is processed into a third part of prompt words according to a preset fixed format.
[0010] Preferably, the complete prompt word is obtained by concatenating the sample prompt word and the prompt word of the code to be processed; The structure of the prompt words in the sample is as follows: in, and These are the code portion and the target output portion of the second code sample group, respectively. This refers to the part of the first prompt word that corresponds to the second code example group; This refers to the part of the second prompt word that corresponds to the second code example group. This refers to the part of the prompt word in the third part that corresponds to the second code example group; The structure of the prompt words in the code to be processed is as follows: in, S This is the task code segment; This refers to the portion of the prompt word in the first part that corresponds to the task code segment. This refers to the part of the prompt text in the second part that corresponds to the task code segment; This refers to the part of the prompt word in the third part that corresponds to the task code segment.
[0011] Preferably, after calculating and filtering the embedding vector similarity of the first code sample group to obtain the second code sample group, the method further includes: The task code segment and the second code sample group are preprocessed; the preprocessing includes removing comments and formatting code.
[0012] Preferably, the step of calculating and filtering the embedding vector similarity of the first code sample group to obtain the second code sample group includes: The first code sample group is processed by a pre-trained model. and task code segment Processed into embedding vectors and ; The embedding vector is obtained through a preset formula. and The processing is performed as cosine similarity of the embedded vectors, and the preset formula is: ; According to cosine similarity Reorder the samples and select the ones with the highest similarity. i One sample is taken as a positive sample, and the sample with the lowest similarity is taken as a negative sample; among them,i The value of is greater than or equal to 2 and less than n; The positive and negative samples are combined to obtain the second code sample group. .
[0013] Secondly, the present invention also provides a large model prompt word optimization system for task code segments, the system comprising: The acquisition module acquires the task code segment input by the user and obtains the corresponding code library based on the task code segment; The first processing module uses the BM25 algorithm to extract n code segments from the code library that are most similar to the code segment of the task as the first code sample group. The second processing module performs embedding vector similarity calculation and filtering on the first code example group to obtain a second code example group; the second code example group includes multiple highly similar code examples and one least similar code example; The third processing module extracts the repository, path, and function name information from the second code sample group and the task code segment to construct the first part of the prompt words; constructs and parses a data flow graph based on the second code sample group and the task code segment to construct the second part of the prompt words; and constructs a syntax tree based on the second code sample group and the task code segment and performs hierarchical analysis to construct the third part of the prompt words. The fourth processing module integrates the first part of the prompt words, the second part of the prompt words, and the third part of the prompt words into a complete prompt word, and then inputs it into the large model to obtain the output result corresponding to the task code segment.
[0014] Thirdly, the present invention also provides a computer-readable storage medium storing a computer program for optimizing large model prompt words for task-oriented code segments, wherein the computer program causes a computer to execute the large model prompt word optimization method for task-oriented code segments as described above.
[0015] Fourthly, the present invention also provides an electronic device, comprising: One or more processors, memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the programs including a large model prompt word optimization method for executing any of the task-oriented code segments described above.
[0016] (III) Beneficial Effects This invention provides a method and system for optimizing large-scale prompt words for task-oriented code segments. Compared with existing technologies, it has the following advantages: This invention first uses BM25 to find a set of code examples in an existing codebase that are close in similarity to the original code of the task code segment to be processed. Then, through embedding vector similarity calculation, the similarity of the obtained examples is re-ranked to obtain multiple examples with high similarity ranking and the example with the lowest similarity ranking in the sequence. The obtained example code and the original code are then processed as follows: First, the repository, path, and function name information contained in the code segment are extracted to form the first part of the prompt words; then, the code is processed to obtain a data flow graph, and the data flow information contained in the data flow graph is used to form the second part of the prompt words; finally, the parameters, methods, classes, modules, and hierarchical information of the code are organized and processed into the third part of the prompt words. The three parts of the prompt words are combined to obtain the prompt words submitted to the large model, resulting in the output of the task code segment. The prompt word optimization of this invention can more fully capture the semantic and structural information of the code, providing a richer context for the large model, thereby improving the processing effect of the task code segment. Attached Figure Description To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 A flowchart illustrating the large model prompt word optimization method provided in this embodiment of the invention; Figure 2 A schematic diagram illustrating the construction process of the second code sample group provided in this embodiment of the invention; Figure 3 A flowchart illustrating the process of generating prompt words for code samples provided in embodiments of the present invention. Detailed Implementation
[0018] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention are described clearly and completely. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0019] This application provides a method and system for optimizing prompt words for large models oriented towards task code segments, which solves the problem of insufficient processing capability of traditional large models when faced with complex task code segments, and realizes the accurate expression of task code segments by large models.
[0020] The technical solution in this application is to solve the above-mentioned technical problems, and the general idea is as follows: Cue words, as a tool to guide large models in understanding task requirements and generating more accurate results, can help models better focus on the key points of the task, thereby improving the quality and efficiency of code generation. However, current cue word generation methods still have the following drawbacks: 1. Traditional prompt generation methods often focus only on the surface description of task code segments, ignoring the deeper semantic and structural information of the code. For example, simply extracting keywords or simple descriptions from the code as prompts cannot fully capture the logical structure and data flow of the code, which may cause large models to fail to accurately understand the true needs of the task when generating code.
[0021] 2. Most existing prompt word generation methods are based on simple text processing techniques and do not fully utilize the pre-training capabilities of large models. For example, they do not incorporate the pre-training tasks of large models to design more effective prompt word generation strategies, resulting in limited quality and effectiveness of the prompt words.
[0022] 3. Existing prompt word generation methods typically focus only on local code snippets or task descriptions, lacking consideration for the global structure of the code. For example, they may simply select some code snippets randomly from the codebase as prompt words without considering the global relationship between these snippets and the original task code segments. This can prevent large models from fully utilizing the global structural information of the code when generating code, affecting the accuracy and consistency of the generated results.
[0023] To better understand the above technical solutions, the following will provide a detailed explanation of the technical solutions in conjunction with the accompanying drawings and specific implementation methods.
[0024] like Figure 1 As shown in the embodiment of this application, a method for optimizing large model prompt words for task-oriented code segments is provided, the method comprising: Step S1: Obtain the task code segment input by the user, and obtain the corresponding code library based on the task code segment.
[0025] Step S2: Based on the BM25 algorithm, extract n code segments from the code library that are most similar to the code segment of the task as the first code sample group.
[0026] Step S3: Calculate and filter the embedding vector similarity of the first code sample group to obtain the second code sample group; the second code sample group includes multiple high-similarity code samples and one low-similarity code sample.
[0027] Step S4: Extract the repository, path, and function name information from the second code sample group and the task code segment to construct the first part of the prompt words; construct and parse a data flow graph based on the second code sample group and the task code segment to construct the second part of the prompt words; construct a syntax tree based on the second code sample group and the task code segment and perform hierarchical analysis to construct the third part of the prompt words.
[0028] Step S5: After integrating the first part of the prompt words, the second part of the prompt words, and the third part of the prompt words into a complete prompt word, input it into the large model to obtain the output result corresponding to the task code segment.
[0029] In one embodiment, step S1 involves obtaining a task code segment input by the user and obtaining the corresponding code library based on the task code segment; Following this embodiment, the following steps are also included: Step S11: Select the corresponding parser and code library according to the programming language type of the task code segment.
[0030] Step S12: The task code segment and the code library are segmented by the parser to obtain a code token.
[0031] In this embodiment, it is important to clarify that the user-input task code segment includes the semantics and structure of the code, as well as the specific requirements of the task. This embodiment performs word segmentation on the code, providing a foundation for subsequent processing.
[0032] In one embodiment, step S2 involves extracting n code snippets from the codebase that are most similar to the task code segment based on the BM25 algorithm, and using these as the first code sample group.
[0033] In this embodiment, a dedicated index is created in the preprocessed codebase using a preset BM25 algorithm to identify the code corresponding to the task code segment. The n code snippets with the closest similarity to the task code segment are then extracted from the codebase as the first code sample group. The BM25 algorithm is a statistical ranking function used to calculate the relevance between a query and a document. The BM25 algorithm is used to calculate the similarity between the processed target code sequence and each document in the document set, and the top n code snippets with the highest similarity are extracted. This serves as the first code example set for subsequent hybrid method processing. In this embodiment, the value of n is greater than or equal to 3.
[0034] In one embodiment, step S3 involves calculating and filtering the embedding vector similarity of the first code sample group to obtain a second code sample group; the second code sample group includes multiple highly similar code samples and one least similar code sample. For example... Figure 2 As shown, Figure 2The process for obtaining code examples is illustrated. The specific implementation of this embodiment includes the following steps: Step S31, using a pre-trained model to process the first code sample group and task code segment Processed into embedding vectors and .
[0035] Step S32, the embedding vector is calculated using a preset formula. and The processing is performed as cosine similarity of the embedded vectors, and the preset formula is: .
[0036] Step S33, according to cosine similarity pair Reorder the samples and select the ones with the highest similarity. i One sample is taken as a positive sample, and the sample with the lowest similarity is taken as a negative sample; among them, i The value of is greater than or equal to 2 and less than n.
[0037] Step S34: Combine the positive samples and the negative samples to obtain the second code sample group. .
[0038] In this embodiment, the first code sample group is trained using a pre-trained model such as BERT or CodeT5. L n and original code S Processed into embedding vectors and The cosine similarity of the embedded vectors is obtained according to a preset formula, where the formula is: According to cosine similarity The samples are reordered, and the top i with the highest similarity are taken as positive samples, while the one with the lowest similarity is taken as a negative sample. These samples are then combined to obtain the second set of code examples. .
[0039] In this embodiment, after obtaining the second code sample group, the method further includes preprocessing the task code segment and the second code sample group; the preprocessing includes removing comments and formatting code.
[0040] In one embodiment, step S4 involves extracting repository, path, and function name information from the second code sample group and the task code segment to construct a first part of the prompt words; constructing and parsing a data flow graph based on the second code sample group and the task code segment to construct a second part of the prompt words; and constructing and performing hierarchical analysis on a syntax tree based on the second code sample group and the task code segment to construct a third part of the prompt words. Figure 3 As shown, Figure 3 This describes the complete process of obtaining the prompt words.
[0041] In this embodiment, repository information, project path names, and function names are obtained from the code repository and task code segment source. The code sample and original code are processed according to a fixed format, such as "Repo:streamsets / datacollector", to obtain the first part of the prompt words, as shown below: In this embodiment, a data flow graph is constructed and parsed based on the second code sample group and the task code segment to construct the second part of the prompt words. The specific implementation steps are as follows: Step S411: Convert the second code sample group and the task code segment into a code attribute graph.
[0042] Step S412: Extract the data flow graph from the code attribute graph.
[0043] Step S413: Traverse and filter the data flow graph to obtain data flow edge information.
[0044] Step S414: Organize the data stream edge information into... A list of data flow edges for the structure; where, srcName It is the starting node of the data stream. from It is the starting node's number in the token sequence. to It is a list of token sequence numbers for all data flow edges originating from the same starting node and ending at the same node.
[0045] Step S415: The data stream side list is processed into a second part of prompt words using a preset format.
[0046] In this embodiment, the Joern tool is used to preprocess the code sample. and original code The process involves converting the graph into a Code Property Graph (CPG) and extracting a Data Flow Graph (DFG) from the generated CPG. Specifically, this involves traversing the DFG and filtering out REACHING_DEF edges between identifier and identifier. Further processing of the DFG involves extracting data flow edge information and organizing edge information originating from the same node. The structure is "[9][11,15,23,25]", where srcName is the starting node of the data flow, from is the starting node's number in the token sequence, and to is a list of token sequence numbers of all the ending nodes of the data flow edges from the same starting node. The data flow edge lists of the code sample and the original code are processed according to a fixed format, for example, "String-[9][11,15,23,25]", to obtain the second part of the prompt word. .
[0047] In this embodiment, a syntax tree is constructed and hierarchical analysis is performed based on the second code sample group and the task code segment to construct the third part of the prompt words, specifically including the following steps: Step S421: Perform abstract syntax tree parsing on the second code sample and the task code segment to obtain variable definition points; the variable definition points include variable declarations, assignments, and return functions.
[0048] Step S422: Extract the node content corresponding to the type of the task code segment from the variable definition point to obtain a node list; the node list includes a function parameter list, a return value list, a method call list, and a method parameter list.
[0049] Step S423: Process the node list into a third part of prompt words according to a preset fixed format.
[0050] In this embodiment, the original code and code examples are provided. The code segments contained within are parsed using an Abstract Syntax Tree (AST) to locate variable definition points (such as variable declaration, assignment, and return); the original code and code examples are analyzed. Perform normal formatting and iterate line by line to obtain the indentation of each line as the level value, resulting in a level value sequence. Traverse the abstract syntax tree, extract the node content of the required type based on the variable definition point type, and obtain the function parameter list. Return value list Method call list Method parameter list The resulting lists are then organized and processed according to a fixed format, such as "Variable declaration:['offsetMap']", to obtain the third set of prompt words. .
[0051] Step S5: After integrating the first part of the prompt words, the second part of the prompt words, and the third part of the prompt words into a complete prompt word, input it into the large model to obtain the output result corresponding to the task code segment.
[0052] In this embodiment, the three parts of the prompt are concatenated and processed into a structure of sample part + code to be processed.
[0053] The structure of the prompt words in the sample is as follows: in, and These are the code portion and the target output portion of the second code sample group, respectively. This refers to the part of the first prompt word that corresponds to the second code example group; This refers to the part of the second prompt word that corresponds to the second code example group. This refers to the part of the prompt word in the third part that corresponds to the second code example group; it should be clarified that the target output part... This is a summary corresponding to the sample code segment, obtained simultaneously with the sample code.
[0054] The structure of the prompt words in the code to be processed is as follows: in, S This is the task code segment; This refers to the portion of the prompt word in the first part that corresponds to the task code segment. This refers to the part of the prompt text in the second part that corresponds to the task code segment; This refers to the part of the prompt word in the third part that corresponds to the task code segment.
[0055] The result and By combining the words, we can obtain the final prompt. Input the large language model to obtain the code task results.
[0056] In this embodiment, by way of example and not limitation, the large model chosen is the GPT3.5-turbo model. For the code summarization task, the training data pairs generally consist of raw code that has already been classified, and code summaries in annotated form. During model training, data is read from the training set and divided into source and target. The source sequences, which serve as input, are embedded and encoded and then input into the model, which is then required to predict the target sequence.
[0057] For the first part, which extracts repository information, project paths, and function names from code samples and the original code, this information should be located on the GitHub repository or in the local storage path where the code originated, as it is information that any code should have. This information for code samples in the code repository comes from a compilation of the original task on GitHub. The first part of the prompt can be organized into a format such as ("Please find some info about the location of the function in the repo.\nRepo:" + train_data[i]['repo']+ "\n" + "Path:" + train_data[i]['path']+ "\n" + "Function_name:" + train_data[i]['func_name']+"\n" + "\n"), and used to concatenate with the subsequent prompts.
[0058] The second part involves constructing and parsing a Data Flow Graph (DFG) from the code examples and the original code to obtain the data flow information contained in the CFG. Specifically, analysis tools such as Joern can be used to process the provided code segment and obtain the CFG. Then, a secondary processing step of edge analysis is performed on the CFG to find edges defined to defined edges, which are then used as edges in the DFG. The data flow graph is then reconstructed to represent the code information. The prompts in the second part can be formatted as, for example, "Please find the dataflow of the function.\nWe persent the source and list of target indices." followed by data flow edges formatted in any fixed way, for concatenation with subsequent prompts.
[0059] The third part involves constructing an Abstract Syntax Tree (AST) and performing hierarchical analysis on the code examples and original code to obtain identifiers such as local variables, function names, parameters, global variables, and level values. The AST construction is implemented using analysis libraries such as Tree-sitter. Hierarchical analysis involves traversing the code segment line by line, obtaining the indentation of each line as a level value, mapping each line's level to the corresponding token group within that line, and processing this into a sequence to obtain the hierarchical sequence of the code segment. Information such as local variables, function names, parameters, and global variables needs to be obtained through a second traversal of the abstract syntax tree. The required nodes are obtained by performing regular expression checks on the node types and relationships with surrounding nodes. The third part of the prompt words can be organized into a structure such as ("We categorized the identifiers into classes.\nPlease find the information below.\nFunction name:" + train_data[i]['func_name']+"\n"+f"Parameters of the function: {parameters}\n"+ f"Identifier to be returned:{identifier}\n"+ f"Method Invocation:{m_i}\n"+ f"Method Arguments:{m_a}\n"+ f"Variable declaration:{var}"+ f"Level:{level}"), which is used to concatenate with the subsequent target part.
[0060] Finally, concatenate the first three parts of the prompt, and add ("\n"+"output:" + train_data[i]['summary']") as the sample target to the end of the prompt in the sample code to obtain the complete prompt for a certain code segment. Note that for code that needs to be predicted, "output:" should still be given as a prompt.
[0061] For gpt3.5-turbo, system-level prompts can be provided as a supplement during tasks. This part of the prompt doesn't need to be entered every time; it's only provided as a task description on the first input, such as "Provide a one-sentence summary of the following code:". For updated models, more complex system-level prompts can be provided, such as ("The user message you receive consists of two parts:\n" "1. Multi-line examples, each ending with the fixed prefix line 'output:'\n" "2. A final new input, also ending with the line 'output:'\n" "Task: Learn the relationship between the example and its output shown in the samples, mimic the style / format found after 'output:' in the examples," "and place the answer for the new input directly after 'output:' without writing 'output:' itself." "Note that the last example provided is of lower similarity and may serve as an anegative example." "Note that the output in the examples is taken from the first line of the func_documentation section of the original code block." "Do not add any explanations, notes, paths, parameter lists, code, line breaks, or Markdown.").
[0062] This invention also provides a large model prompt word optimization system for task-oriented code segments, the system comprising: The acquisition module acquires the task code segment input by the user and obtains the corresponding code library based on the task code segment.
[0063] The first processing module uses the BM25 algorithm to extract n code segments from the codebase that are most similar to the code segment of the task as the first code sample group.
[0064] The second processing module performs embedding vector similarity calculation and filtering on the first code sample group to obtain a second code sample group; the second code sample group includes multiple highly similar code samples and one least similar code sample.
[0065] The third processing module extracts the repository, path, and function name information from the second code sample group and the task code segment to construct the first part of the prompt words; it constructs and parses a data flow graph based on the second code sample group and the task code segment to construct the second part of the prompt words; and it constructs a syntax tree based on the second code sample group and the task code segment and performs hierarchical analysis to construct the third part of the prompt words.
[0066] The fourth processing module integrates the first part of the prompt words, the second part of the prompt words, and the third part of the prompt words into a complete prompt word, and then inputs it into the large model to obtain the output result corresponding to the task code segment.
[0067] It is understood that the task-oriented code segment large model prompt word optimization system provided in this embodiment of the invention corresponds to the above-mentioned task-oriented code segment large model prompt word optimization method. The explanation, examples, and beneficial effects of the relevant content can be referred to the corresponding content in the task-oriented code segment large model prompt word optimization method, and will not be repeated here.
[0068] This invention also provides a computer-readable storage medium storing a computer program for a large model prompt word optimization method for task-oriented code segments, wherein the computer program causes a computer to execute the large model prompt word optimization method for task-oriented code segments as described above.
[0069] This application also provides an electronic device, including: one or more processors; a memory; and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the programs including a large model prompt word optimization method for performing task-oriented code segments as described above.
[0070] In summary, compared with existing technologies, it has the following beneficial effects: 1. This invention generates more comprehensive prompts by extracting multi-dimensional information from the code, including its repository, path, function name, data flow graph, parameters, methods, classes, modules, and hierarchies. Compared to existing technologies that only focus on code snippets or simple task descriptions, this method can more fully capture the semantic and structural information of the code, providing richer context for large models and thus improving the processing effect of task code segments.
[0071] 2. This invention not only considers local code information but also introduces global code structure information through techniques such as data flow graphs and abstract syntax trees. Compared to existing technologies that only focus on local code fragments, this method helps large models better understand the overall structure and logic of the code, generating more accurate task code segment results.
[0072] 3. This invention, through a carefully designed prompt word generation method, effectively integrates the semantic and structural information of the code into the prompt words, thereby enhancing the large model's ability to understand the code. Compared to the simple, manually designed prompt words in existing technologies, this method can significantly improve the model's adaptability and performance to task code segments.
[0073] 4. This invention combines the embedding vector similarity calculation of the BM25 model and the pre-trained model to quickly filter out code examples with high similarity to the original code, and further optimizes the prompt word generation process. Compared with existing technologies, this method can reduce the time cost of prompt word generation and improve coding efficiency.
[0074] 5. The prompt word generation method of this invention can fully utilize the rich information in a large-scale codebase and improve the model's adaptability to different programming languages and tasks through the generalization ability of the pre-trained model. Compared with existing technologies, this method can better support cross-language and multi-task applications and improve the model's generalization ability.
[0075] 6. The prompt word generation method of this invention is not only applicable to code summarization tasks, but can also be widely applied to various code-related tasks such as code generation, code recommendation, and code auto-completion. In these tasks, the method of this invention can significantly improve the performance and effectiveness of the model, better meeting the diverse needs of actual development.
[0076] It should be noted that, in this document, relational terms such as "first" and "second" are used only 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 said element.
[0077] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for optimizing large model prompt words for task-oriented code segments, characterized in that, The method includes: Obtain the task code segment input by the user, and obtain the corresponding code library based on the task code segment; Based on the BM25 algorithm, n code segments with the closest similarity to the task code segment are extracted from the code library as the first code sample group. The embedding vector similarity of the first code sample group is calculated and filtered to obtain the second code sample group; the second code sample group includes multiple highly similar code samples and one least similar code sample; Extract the repository, path, and function name information from the second code sample group and the task code segment to construct the first part of the prompt words; construct and parse a data flow graph based on the second code sample group and the task code segment to construct the second part of the prompt words; construct a syntax tree based on the second code sample group and the task code segment and perform hierarchical analysis to construct the third part of the prompt words; After integrating the first part of the prompt words, the second part of the prompt words, and the third part of the prompt words into a complete prompt word, the input is given to the large model to obtain the output result corresponding to the task code segment.
2. The method for optimizing large model prompt words for task-oriented code segments according to claim 1, characterized in that, After obtaining the task code segment input by the user and obtaining the corresponding code library based on the task code segment, the method further includes: Select the corresponding parser and code library based on the programming language type of the task code segment; The task code segment and the code library are segmented by the parser to obtain a code token.
3. The method for optimizing large model prompt words for task-oriented code segments according to claim 2, characterized in that, The step of constructing and parsing a data flow graph based on the second code sample group and the task code segment, and constructing the second part of the prompt words, includes: Convert the second code sample group and the task code segment into a code attribute graph; Extract the data flow graph from the code attribute graph; The data flow graph is traversed and filtered to obtain data flow edge information; The data flow edge information is organized as follows: A list of data flow edges for the structure; where, srcName It is the starting node of the data stream. from It is the starting node's number in the token sequence. to It is a list of token sequence numbers of the end nodes of all data flow edges originating from the same starting node; The data stream side list is processed into a second part of prompt words using a preset format.
4. The method for optimizing large model prompt words for task-oriented code segments according to claim 2, characterized in that, The step of constructing a syntax tree and performing hierarchical analysis based on the second code sample group and the task code segment to construct the third part of the prompt words includes: The second code sample group and the task code segment are parsed using an abstract syntax tree to obtain variable definition points; the variable definition points include variable declarations, assignments, and return functions. Extract the node content corresponding to the type of the task code segment from the variable definition point to obtain a node list; the node list includes a function parameter list, a return value list, a method call list, and a method parameter list; The node list is processed into a third part of prompt words according to a preset fixed format.
5. The method for optimizing large model prompt words for task-oriented code segments according to claim 2, characterized in that, The complete prompt is obtained by concatenating the prompt from the sample portion and the prompt from the code to be processed. The structure of the prompt words in the sample is as follows: in, and These are the code portion and the target output from the second code sample group, respectively. This refers to the part of the first prompt word that corresponds to the second code example group; This refers to the part of the second prompt word that corresponds to the second code example group. This refers to the part of the prompt word in the third part that corresponds to the second code example group; The structure of the prompt words in the code to be processed is as follows: in, S This is the task code segment; This refers to the portion of the prompt word in the first part that corresponds to the task code segment. This refers to the part of the prompt text in the second part that corresponds to the task code segment; This refers to the part of the prompt word in the third part that corresponds to the task code segment.
6. The method for optimizing large model prompt words for task-oriented code segments according to claim 2, characterized in that, After calculating and filtering the embedding vector similarity of the first code sample group to obtain the second code sample group, the method further includes: The task code segment and the second code sample group are preprocessed; the preprocessing includes removing comments and formatting code.
7. The method for optimizing large model prompt words for task-oriented code segments according to claim 1, characterized in that, The step of calculating and filtering the embedding vector similarity of the first code sample group to obtain the second code sample group includes: The first code sample group is processed by a pre-trained model. and task code segment Processed into embedding vectors and ; The embedding vector is obtained through a preset formula. and The processing is performed as cosine similarity of the embedded vectors, and the preset formula is: ; According to cosine similarity Reorder the samples and select the ones with the highest similarity. i One sample is taken as a positive sample, and the sample with the lowest similarity is taken as a negative sample; among them, i The value of is greater than or equal to 2 and less than n; The positive and negative samples are combined to obtain the second code sample group. .
8. A large-model prompt word optimization system for task code segments, characterized in that, The system includes: The acquisition module acquires the task code segment input by the user and obtains the corresponding code library based on the task code segment; The first processing module uses the BM25 algorithm to extract n code segments from the code library that are most similar to the code segment of the task as the first code sample group. The second processing module performs embedding vector similarity calculation and filtering on the first code example group to obtain a second code example group; the second code example group includes multiple highly similar code examples and one least similar code example; The third processing module extracts the repository, path, and function name information from the second code sample group and the task code segment to construct the first part of the prompt words; constructs and parses a data flow graph based on the second code sample group and the task code segment to construct the second part of the prompt words; and constructs a syntax tree based on the second code sample group and the task code segment and performs hierarchical analysis to construct the third part of the prompt words. The fourth processing module integrates the first part of the prompt words, the second part of the prompt words, and the third part of the prompt words into a complete prompt word, and then inputs it into the large model to obtain the output result corresponding to the task code segment.
9. A computer-readable storage medium, characterized in that, It stores a computer program for optimizing large model prompts for task-oriented code segments, wherein the computer program causes a computer to execute the large model prompt optimization method for task-oriented code segments as described in any one of claims 1 to 7.
10. An electronic device, characterized in that, include: One or more processors, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the programs including a large model prompt word optimization method for performing task-oriented code segments as described in any one of claims 1 to 7.