Code snippet automatic adaptation method and device based on large language model agent interaction
By constructing an automatic code snippet adaptation device based on a large language model-based intelligent agent interaction method, the problem of time-consuming and labor-intensive code snippet adaptation process is solved, and extensive adaptation without historical data dependence and efficient and accurate code snippet generation are achieved.
Patent Information
- Application Number
- CN202411690255.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-22
- Publication Date
- 2025-12-26
- Estimated Expiration
- 2044-11-22
AI Technical Summary
The existing code snippet adaptation process is time-consuming and labor-intensive for developers, and existing automated methods have problems such as limitations in the types of adaptation and reliance on historical data.
A large language model-based agent interaction method is adopted. By constructing a prompt word module and an agent, including code context, code snippet retrieval, requirement refinement and adaptation output modules, the adapted code snippet is generated through multi-round interaction between the executor and the advisor agent.
It achieves broad adaptability without relying on historical data, reduces manual costs, and improves the accuracy and efficiency of code snippet adaptation, making it suitable for various code snippet recommendations.
Smart Images

Figure CN119536721B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of code snippet adaptation, in particular to a code snippet automatic adaptation method and device based on large language model agent interaction. BACKGROUND
[0002] With the development of software open source community, code snippet reuse has become an important practice in modern software development. By reusing existing software solutions, developers can avoid repetitive development processes, efficiently complete software delivery and ensure software quality. These code snippets from various open source communities (such as Stack Overflow, GitHub, etc.) are reviewed and tested by many open source contributors, so they have strong usability and reliability. However, due to the different contexts of code snippets in open source communities and developers' software systems, developers need to adapt the code snippets to make them function in their own software systems and meet the corresponding software development needs. The adaptation process is a time-consuming and energy-consuming task for developers, and current research has proposed automatic adaptation methods to improve the efficiency of the adaptation process. Some people analyze and solve specific types of adaptation, such as Cottrell et al. who proposed the Jigsaw tool to solve variable identifier renaming through code structure correspondence. Terragni et al. conducted an empirical study on a special type of adaptation, "APIization", which is to encapsulate an independent code snippet into a format-specific method. They proposed four types of "APIization" patterns and implemented the APIZATOR tool to automatically recommend "APIized" code for reuse. Another group of researchers recommends the required adaptation to developers based on historical data. Zhang et al. analyzed the adaptation history of developers who reused code snippets from Stack Overflow open source community in GitHub projects and proposed the ExampleStack tool to recommend adaptation solutions for corresponding code snippets. SUMMARY
[0003] Therefore, it is necessary to provide a code snippet automatic adaptation method and device based on large language model agent interaction to solve the above technical problems.
[0004] A code snippet automatic adaptation method based on large language model agent interaction, the method comprising:
[0005] constructing a prompt word module of a large language model according to the requirement information expressed in natural language, the retrieved code snippet and the class-level context of the developer; the prompt word module comprises a code context module, a retrieved code snippet module, a requirement refinement module and an adaptation output module;
[0006] The big language model is used to construct an executor agent and a consultant agent.
[0007] The code context module is used to initialize the big language model-based agent, the retrieval code segment module and the requirement refinement module are spliced as the input of the executor agent, if the adapted code segment cannot be generated, the question raised by the executor agent is transmitted to the consultant agent as the prompt input, the answer of the consultant agent to the question is inserted into the adaptation output module as the updated input of the executor agent, and the adapted code segment is output by the executor agent according to the updated input and the historical interaction information.
[0008] In one of the embodiments, the code context module is used to provide the prompt words containing the import statement in the extracted developer code and the class where the code segment to be adapted is located.
[0009] In one of the embodiments, the retrieval code segment module is used to provide the prompt words containing the code segment to be reused extracted from the code retrieval result.
[0010] In one of the embodiments, the requirement refinement module is used to provide the prompt words containing the requirement refinement instruction and the user requirement specification.
[0011] In one of the embodiments, the requirement refinement module is used to guide the consultant agent to answer the question raised by the executor agent through the instruction, and extract the software development requirement hidden in the code.
[0012] In one of the embodiments, the adaptation output module is used to provide the prompt words combined with the answer of the consultant agent to the question, the output instruction and the adaptation dependency.
[0013] In one of the embodiments, in the first round of interaction, the code context module is used as the prompt input of the big language model, so that the big language model-based agent has the code context understanding ability.
[0014] In one of the embodiments, in the second round of interaction, the code context understanding ability of the executor agent is used to understand and infer the generated code segment, if the adapted code segment is generated, the interaction is terminated and the adapted code segment is output, if not, the executor agent generates a question list and selects a question from the question list as the prompt input transmitted to the consultant agent.
[0015] In one of the embodiments, in the third interaction, the answer of the consultant agent to the question is inserted into the adaptation output module, an updated input is obtained, the updated input and the historical interaction information are taken as the executor agent input, and the adapted code snippet or feedback of the unrecognized successful result is output; the output result is inserted into the target context of the user, so as to verify the correctness of the generated code snippet.
[0016] An automatic code snippet adaptation device based on large language model agent interaction, the device comprises:
[0017] A prompt word template construction module is configured to construct a prompt word module of a large language model according to natural language expressed requirement information, retrieved code snippets and class level context of a developer; the prompt word module comprises a code context module, a retrieved code snippet module, a requirement refinement module and an adaptation output module.
[0018] An agent construction module is configured to construct an executor agent and a consultant agent based on a large language model.
[0019] An interaction module is configured to initialize the agent based on the large language model with the code context module as input, splice the retrieved code snippet module and the requirement refinement module as input of the executor agent, if an adapted code snippet cannot be generated, pass a question raised by the executor agent to the consultant agent as prompt input, and insert an answer of the consultant agent to the question into the adaptation output module as updated input of the executor agent, so that the executor agent outputs an adapted code snippet according to the updated input and historical interaction information.
[0020] The above-mentioned automatic code snippet adaptation method and device based on large language model agent interaction utilize the task generalization ability learned by the large language model in the pre-training stage, and for any adaptation task described in natural language, an adapted code can be automatically generated as an output result, which is not limited to specific adaptation such as variable identifier replacement and API in existing methods, and has a wide range of application scenarios. The present application does not depend on any historical data, and for any code snippet in the Stack Overflow community, an adapted code can be generated and recommended to the developer, and does not depend on any manual participation, and the developer only needs to review whether the output of the method meets the target requirement, thereby saving a large amount of manual cost. Through multi-round interaction based on the agent, the large language model refines the requirement based on the code context of the developer, and utilizes intermediate results such as dependency information to assist in result generation, which can improve the accuracy of the method. BRIEF DESCRIPTION OF DRAWINGS
[0021] Figure 1A flowchart of an embodiment of a code snippet automatic adaptation method based on large language model agent interaction;
[0022] Figure 2 A framework diagram of an embodiment of a code snippet automatic adaptation method based on large language model agent interaction;
[0023] Figure 3 A baseline method schematic diagram in an embodiment;
[0024] Figure 4 A structural schematic diagram of an embodiment of a code snippet automatic adaptation device based on large language model agent interaction. DETAILED DESCRIPTION
[0025] In order to make the purpose, technical scheme and advantages of the present application clearer, the present application will be further described in detail below in combination with the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and do not limit the present application.
[0026] In one embodiment, as shown in Figure 1 A code snippet automatic adaptation method based on large language model agent interaction is provided, comprising:
[0027] Step 102, constructing a prompt word module of a large language model according to requirement information expressed in natural language, retrieved code snippets and the class level context of the developer.
[0028] The prompt word module includes a code context module, a retrieved code snippet module, a requirement refinement module and an adaptation output module.
[0029] Step 104, constructing an executor agent and a consultant agent based on a large language model.
[0030] Step 106, initializing the agent based on a large language model with the code context module as input, concatenating the retrieved code snippet module and the requirement refinement module as input of the executor agent, if an adapted code snippet cannot be generated, passing the question raised by the executor agent to the consultant agent as a prompt input, and inserting the answer of the consultant agent to the question into the adaptation output module as an updated input of the executor agent, so that the executor agent outputs an adapted code snippet according to the updated input and historical interaction information.
[0031] The aforementioned automatic code snippet adaptation method based on large language model agent interaction leverages the task generalization ability learned by the large language model during the pre-training phase. For any adaptation task described in natural language, it can automatically generate adapted code as output, not just limited to specific adaptations such as variable identifier replacement and API-ization in existing methods, thus having a wide range of applications. This invention does not rely on any historical data; for any code snippet from the Stack Overflow community, it can generate adapted code to recommend to developers, and it does not require any human intervention. Developers only need to review whether the method's output meets the target requirements, saving significant labor costs. Through multi-turn agent-based interaction, the large language model refines the requirements based on the developer's code context and uses intermediate results such as dependency information to assist in result generation, thereby improving the method's accuracy.
[0032] The framework of this method is as follows: Figure 2 As shown, this method first constructs a corresponding prompt word framework and module based on the needs of natural language expression, the retrieved code snippets, and the developer's class-level context. This framework is then input into an intelligent agent interaction system based on a large language model. Leveraging the powerful natural language understanding and generation capabilities of the large language model, the system generates adapted code snippets through a series of intelligent agent interactions and integrates them into the developer's context. In this process, the tasks of understanding requirements and adapting execution, originally performed by the developer, are all handled by the intelligent agent played by the large language model, saving developers' manual labor costs. Furthermore, this method utilizes the task generalization capabilities gained by the large language model during the pre-training phase, eliminating limitations on adaptation types and allowing for broad application to code snippet adaptation tasks.
[0033] The natural language instructions provided by users to a large language model are typically referred to as "hint words," and different hint words have a significant impact on the performance of the large language model in the task. This invention designs a unique hint word framework for code snippet adaptation tasks to maximize the capabilities of the large language model and solve the problem of automated code snippet adaptation. Specifically, the hint words provided to the large language model mainly consist of two parts: the first part is system hints, used for global initialization of the large model; the second part is instruction hints, serving as the input to the large language model. Since the large language model supports multi-turn interactions, the instruction hints provided by the user in each turn of interaction with the large language model not only include the current input but also historical interaction messages, i.e., multiple sets of user message input-large language model output pairs.
[0034] In one embodiment, the code context module is used to provide prompts containing import statements from the extracted developer code and the class in which the code snippet to be adapted belongs.
[0035] Specifically, the purpose of the code snippet reuse is to make it run normally in the developer's code context and achieve its corresponding function, so the developer's code context is crucial for the adaptation of the code snippet. The present application extracts the import statement in the developer's code, and the class where the code snippet to be adapted is located (including class name, class description and all methods currently implemented) to form a code context prompt word module. Due to the pre-training process of the large language model, the input content is often analyzed and explained, such as for the provided code context, the large language model will output a series of explanations about the code function, which may help the developer to better understand the input code, but it is not directly helpful for the automatic adaptation method of the code snippet, on the contrary, it will bring more token overhead to the method. Therefore, the following instructions are added after the module: "Above is a class context of `[CLASS_NAME]`, please understand and remember it but do not reply to this message, do you understand?" (translated as: "The above is the context of a certain class, please try to understand and remember it, but do not reply to this message, do you understand?") to reduce the invalid output of the large language model and save resource overhead.
[0036] In one of the embodiments, the retrieval code snippet module is used to provide a prompt word containing the code snippet to be reused extracted from the code retrieval result.
[0037] Specifically, in the code snippet adaptation task, the developer will retrieve code snippets from the Internet, open source software communities or generate them with the help of code recommendation tools to meet the development needs of users, and then reuse these code snippets to meet the corresponding development needs. Therefore, the present application extracts the corresponding code snippet to be reused from the developer's retrieval or generation results to form a retrieval code prompt word module as an adaptation object of the large language model.
[0038] In one of the embodiments, the requirement refinement module is used to provide a prompt word containing requirement refinement instructions and user requirement specifications.
[0039] Specifically, in the intelligent agent interaction process design, the application requires the intelligent agent based on the large language model to refine the demand proposed by the user, and the prompt word module used in the process mainly includes instructions and user demand specifications. The first half of the instruction is "I would like you to ask me at most 3 questions to retrieve necessary information to adapt the above code snippet `[METHOD_NAME]` to the class context of `[CLASS_NAME]`." (Translation: "I want you to ask me at most 3 questions to retrieve necessary information to adapt the above code snippet `[METHOD_NAME]` to the class context of `[CLASS_NAME]`.") The intelligent agent based on the large language model is required to "turn over" its role, from a passive question answerer to an active questioner, to express the abstract software development demand hidden in the code in the form of natural language question and answer, which is more conducive to the large language model to complete the automatic adaptation task. For the parameter setting (number of questions), pilot experiments were conducted on 20 randomly selected adaptation tasks, and it was found that if the number of questions of the large language model is not limited, it will cause repeated confirmation of some explicit development requirements, resulting in invalid token overhead. Therefore, according to the average number of questions of the adaptation task, which is 3.2, the upper limit of the number of questions of the large language model in this module is determined. For the user demand specification part of this module, the method signature of the code to be adapted, the natural language demand and the input-output pair are included to comprehensively and accurately describe the user's development demand.
[0040] In one of the embodiments, the adaptation output module provides a response to the question, an output instruction and a prompt word about the adaptation dependency in combination with the consultant intelligent agent.
[0041] Specifically, the adaptation output module contains two parts, the first part includes the answer to the question asked to the large language model, and the second part includes the output instruction and the dependent information about the adaptation. For the output of the large language model, there may be conflicts with the code context, and the idea of thinking chain is used to provide the intermediate steps for the adaptation, that is, to realize the required dependencies, to improve the accuracy of the adaptation. If the adaptation process depends on specific elements in the code context, the final instruction is "It should be implemented using libraries: [LIBRARIES], fields: [FIELDS] methods: [METHODS]." If the adaptation process does not depend on any code elements, the instruction is "It should be implemented without using any external libraries, member variables, or methods."
[0042] After the establishment of the above prompt word module, the agent interaction process design will be carried out. The large language model shows certain learning, reasoning and decision-making ability, and can be used as an independent agent to independently perceive the environment, make decisions and perform corresponding behaviors. Due to the complexity of the code snippet adaptation task, the demand acquisition process of users and software developers in software engineering is borrowed, and the corresponding agent is constructed based on the large language model to play a specific role, and the task is completed through the cooperation between agents. The present application constructs two different agents based on the large language model, the first one is the executor agent, the main responsibilities include: 1) if the large language model perceives that the demand proposed by the user has information missing or confusion, generate the corresponding question to refine and confirm the demand proposed by the user; 2) if the large language model thinks that the current information is enough to execute the adaptation, generate the adapted code snippet according to the user's demand and the explanation of the question proposed by the user. The second one is the consultant agent, the main responsibility is to reply to the question proposed by the executor agent.
[0043] In one of the embodiments, due to the complexity of the elements contained in the adaptation task, constructing all the prompt word modules into a single input provided to the large language model will cause the input message to be too long, thereby causing the accuracy of the large language model to decrease for the understanding of the input. Therefore, the task to be executed by the agent is decomposed, and the task description is provided to the large language model inside the agent through multiple rounds of instruction input. In the first round of interaction, the code context module is input as a prompt to the large language model-based agent, so that the agent has the ability to understand the code context, thereby reducing the difficulty of the large language model inside the agent to understand the context of the super-long text. Since the historical interaction messages are input during the interaction with the agent in each round, it is also ensured that this part of information will not be lost in the subsequent interaction process.
[0044] In one of the embodiments, in the second round of interaction, the code context understanding ability of the executor agent is used to understand and infer the generated code snippet; if the adapted code snippet is generated, the interaction is terminated and the adapted code snippet is output; if not, the executor agent generates a question list and selects a question from the question list as a prompt input to the consultant agent.
[0045] Specifically, in the second round of interaction, the retrieval code snippet module and the requirement refinement module are spliced to provide the large language model as a prompt input, and the executor agent is required to use the "reflection" ability to understand and infer based on the previous code context, the retrieved code snippet, and the requirement to be implemented to determine whether the adaptation can be completed. If yes, the adapted code snippet is directly generated, and the code snippet is identified at this time and the interaction is terminated; if not, a series of questions are generated as output. If the adaptation is not completed, the questions raised by the executor agent are extracted as a prompt input to the consultant agent, which is required to answer these questions according to the code context and output.
[0046] In another embodiment, in the third interaction, the answers of the consultant agent to the questions are inserted into the adaptation output module to obtain an updated input, the updated input and the historical interaction information are input to the executor agent, and the adapted code snippet or the feedback of the unrecognized successful result is output. The output result is inserted into the target context of the user, so as to verify the correctness of the generated code snippet.
[0047] Specifically, the adaptation output module provides the large language model of the executor agent as input, which is required to output the adapted code snippet based on the previous interaction history according to the answers of the consultant agent. Similar to the processing of the output result of the executor agent in the second round of interaction, the adapted code snippet in the output is identified, and if the identification is not successful, an empty string is returned as the result.
[0048] The code snippet generation is completed, and the beneficial effects of the application are described through experiments as follows.
[0049] The ClassEval benchmark is used to evaluate the adaptation performance of the application. ClassEval is a class-level code generation benchmark that contains 100 Python classes and their related test suites. Because it contains class-level code, it can effectively test code snippet adaptation for such context-dependent tasks. In addition, ClassEval is a manually constructed dataset covering a variety of topics, simulating real software development scenarios. The dataset was released in 2023, so it can reduce the problem of large language model training data leakage. Based on ClassEval, the class-level context is obtained by excluding the target method and all dependent methods (i.e., calling methods) in each class. Finally, by pairing the retrieved code snippets with their corresponding contexts, 200 method-level code snippet adaptation tasks are obtained.
[0050] Three widely studied large language models are selected, including a general-purpose large language model GPT-3.5 and a instruction fine-tuned code large language model CodeLlama. For GPT-3.5, the model of gpt-3.5-turbo-0613 checkpoint is accessed through OpenAI API. For CodeLlama, its 34B (with about 3.4 billion parameters) version is selected.
[0051] Since there is currently no universal code snippet automatic adaptation method, a prompt method based on large language models is designed as a baseline, using prompts such as Figure 3 as shown in the upper half.
[0052] In addition, in order to explore the effectiveness of the agent interaction in the application, the executor agent is used to generate adapted code through multiple rounds of interaction, and its prompt process is as follows Figure 3 as shown in the lower half.
[0053] pass@k is used as the main indicator to evaluate the correctness of large language model adaptation. It measures the probability of passing all unit tests within k attempts, and the formula is as follows:
[0054]
[0055] where k represents the number of candidates sampled from all results, the total number is n, and c is the number of samples that pass the test. Considering the generation cost, n is set to 5. Specifically, pass@1 and pass@5 are calculated for each adaptation task, reflecting accuracy and actual solution discovery efficiency, i.e., whether a solution can be found by browsing no more than 5 large language model adapted code snippets.
[0056] In addition, CodeBLEU is also used as a supplementary evaluation indicator. It matches the syntax, syntax and semantic similarity between the output and the correct results by n-gram matching, abstract syntax tree and data flow comparison. It is used to evaluate the similarity between the generated fragments and the standard solution.
[0057] In order to alleviate the influence of the randomness of the large language model output, the output is sampled according to the method of kernel sampling. Specifically, for each task, 5 output results are randomly generated and collected for evaluation. The experiment also discusses the influence of the temperature setting of the large language model on its performance, and determines the optimal temperature through grid search. The temperature value is from 0 to 1, with an interval of 0.2. The optimal temperature of GPT-3.5 is 0.8, and the optimal temperature of CodeLlama is 0.8. The maximum token window of all large language models is set to 2,048 to ensure consistent context length. All experiments are carried out on two GeForce RTX 4090-24G GPUs.
[0058] Table 1 compares the performance of the large language model agent interaction method and the baseline method on the adaptation task
[0059]
[0060] Table 1 shows the effectiveness of the present application compared with the baseline method. For the GPT-3.5 model, the large language model agent interaction method achieves the best results on the pass@1 and pass@5 indicators, which are 71.80 and 82.50, respectively. This means that the present application has more than 70% accuracy on the code snippet adaptation task. Compared with the GPT-3.5-Base method, which writes a simple prompt about the adaptation task to guide the large model to complete the adaptation, the present application has 21.80%, 26.00% and 12.51% improvement in the pass@1, pass@5 and CodeBLEU indicators, respectively. Compared with the GPT-3.5-SingleAgent method, which interacts with a single agent in multiple rounds, the present application has 3.3% and 9% improvement in pass@1 and pass@5, although it has a slight decrease in the CodeBLEU indicator. The reason may be that the present application considers the suggestions of the consultant agent when outputting the results, which reduces the code similarity, but does not affect the accuracy of the method itself. For the CodeLlama model, it achieves the best results on the three evaluation indicators, which are 45.95, 67.32 and 45.58, respectively, at least 8% and 2% higher than the Base and SingleAgent methods.
[0061] In summary, the code snippet adaptation problem is a task that requires a lot of manual cost to complete in the field of software engineering. In the present application, the problem of automatic adaptation of code snippets is solved using a large language model-based agent interaction method. Specifically, the task designs a prompt framework and module, which can automatically generate prompt words as input for the large language model for each adaptation task. In addition, the powerful natural language and code understanding and generation capabilities of the large language model are used to play different agent roles, and through a multi-round interaction process, the large language model simulates human developers to solve the code snippet adaptation problem and generate the adapted code snippet. Finally, experiments are conducted on the ClassEval benchmark and two large models to verify the effectiveness of the present application compared to the simple prompt word method.
[0062] In one embodiment, as shown in Figure 4 A large language model agent interaction-based code snippet automatic adaptation apparatus is provided, comprising a prompt word template construction module 402, an agent construction module 404, and an interaction module 406.
[0063] The prompt word template construction module 402 is configured to construct a prompt word module of the large language model according to the requirement information expressed in natural language, the retrieved code snippet, and the class-level context of the developer; the prompt word module comprises a code context module, a retrieved code snippet module, a requirement refinement module, and an adaptation output module.
[0064] The agent construction module 404 is configured to construct an executor agent and a consultant agent based on the large language model;
[0065] The interaction module 406 is configured to initialize the agent based on the large language model with the code context module as input, splice the retrieved code snippet module and the requirement refinement module as input of the executor agent, if the adapted code snippet cannot be generated, pass the question raised by the executor agent to the consultant agent as prompt input, and insert the answer of the consultant agent to the question into the adaptation output module as updated input of the executor agent, so that the executor agent outputs the adapted code snippet according to the updated input and historical interaction information.
[0066] The technical features of the above embodiments can be combined in any way. To make the description concise, not all possible combinations of the technical features in the above embodiments are described, but as long as the combinations of the technical features do not exist, they should be considered as the scope of the present application.
[0067] The above-described embodiments are merely illustrative of several embodiments of the present application, which are described in more detail and in a specific and detailed manner, but should not be construed as limiting the scope of the patent. It should be noted that for those skilled in the art, several modifications and improvements can be made without departing from the concept of the present application, and these are all within the scope of the present application. Therefore, the scope of protection of the patent of the present application should be subject to the appended claims.
Claims
1. A code snippet automatic adaptation method based on large language model agent interaction, characterized in that, The method comprises: According to the demand information expressed in natural language, the retrieved code segment and the class-level context of the developer, a prompt word module of a large language model is constructed; the prompt word module comprises a code context module, a retrieved code segment module, a demand refinement module and an adaptive output module; An executor agent and a consultant agent are constructed based on the large language model; The code context module is used as input to initialize the agent based on the large language model, the retrieved code segment module and the demand refinement module are spliced as input of the executor agent, if an adapted code segment cannot be generated, the question raised by the executor agent is transmitted as prompt input to the consultant agent, and the answer of the consultant agent to the question is inserted into the adaptive output module as the update input of the executor agent, so that the executor agent outputs the adapted code segment according to the update input and historical interaction information; The code context module is used to provide prompt words containing import statements in the extracted developer code and the class where the code segment to be adapted is located; the import statements in the developer code and the class where the code segment to be adapted is located constitute the code context prompt word module; The retrieved code segment module is used to provide prompt words containing the code segment to be reused extracted from the code retrieval result; the code segment to be reused is extracted from the developer's retrieval result or generation result to constitute the retrieval code prompt word module as the adaptation object of the large language model.
2. The method of claim 1, wherein, The demand refinement module is used to provide prompt words containing demand refinement instructions and user demand specifications.
3. The method of claim 2, wherein, The demand refinement module guides the consultant agent to answer the questions of the executor agent through instructions, and extracts the software development demand hidden in the code.
4. The method of claim 1, wherein, The adaptive output module is used to provide prompt words combined with the answer of the consultant agent to the question, the output instruction and the adaptation dependency.
5. The method according to any one of claims 1 to 4, characterized in that, The code context module is used as input to initialize the agent based on the large language model, comprising: In the first round of interaction, the code context module is used as prompt input of the agent based on the large language model, so that it has the ability to understand the code context.
6. The method of claim 5, wherein, The retrieved code segment module and the demand refinement module are spliced as input of the executor agent, if an adapted code segment cannot be generated, the question raised by the executor agent is transmitted as prompt input to the consultant agent, comprising: In the second round of interaction, the code context understanding ability of the executor agent is used to understand and reason to generate a code segment; If an adapted code segment is generated, the interaction is terminated and the adapted code segment is outputted; If not, the executor agent generates a question list and selects a question from the question list as prompt input to the consultant agent.
7. The method of claim 6, wherein, The answer of the consultant agent to the question is inserted into the adaptive output module as the update input of the executor agent, so that the executor agent outputs the adapted code segment according to the update input and historical interaction information, comprising: In the third interaction, the answer of the consultant agent to the question is inserted into the adaptation output module, obtaining an updated input, and the updated input and historical interaction information are taken as the executor agent input, and an adapted code snippet or feedback of a non-recognized successful result is outputted; The output result is inserted into the target context of the user, so as to verify the correctness of the generated code snippet.
8. An automatic code snippet adaptation device based on large language model agent interaction, characterized in that, The device comprises: A prompt word template construction module is configured to construct a prompt word module of a large language model according to the requirement information expressed in natural language, the retrieved code snippet and the class-level context of the developer; the prompt word module comprises a code context module, a retrieved code snippet module, a requirement refinement module and an adaptation output module; An agent construction module is configured to construct an executor agent and a consultant agent based on the large language model; An interaction module is configured to initialize the agent based on the large language model by taking the code context module as the input, splice the retrieved code snippet module and the requirement refinement module as the input of the executor agent, pass the question raised by the executor agent to the consultant agent as the prompt input if an adapted code snippet cannot be generated, and insert the answer of the consultant agent to the question into the adaptation output module as the updated input of the executor agent, so that the executor agent outputs an adapted code snippet according to the updated input and historical interaction information. The code context module is configured to provide a prompt word comprising the import statement in the extracted developer code and the class in which the code snippet to be adapted is located; the import statement in the developer code and the class in which the code snippet to be adapted is located constitute a code context prompt word module. The retrieved code snippet module is configured to provide a prompt word comprising the code snippet to be reused extracted from the code retrieval result; the code snippet to be reused extracted from the retrieval result or the generation result of the developer constitutes a retrieved code prompt word module, which is taken as an adaptation object of the large language model.
Citation Information
Patent Citations
Method and system for automatically generating reusable API based on code snippets
CN117892031A
Natural language intelligent query method and device based on multi-agent interaction
CN118012900A