Code snippet automatic adaptation method and device based on large language model agent interaction

Through the method based on the interaction of agents based on the large language model, automatic adaptation of code fragments is achieved, solving the problem of time-consuming and reliance on manual participation in the existing technology, and improving the adaptation efficiency and accuracy.

CN119536721AActive Publication Date: 2025-02-28NAT UNIV OF DEFENSE TECH

Patent Information

Application Number
CN202411690255.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-22
Publication Date
2025-02-28
Estimated Expiration
2044-11-22

AI Technical Summary

Technical Problem

In the prior art, the code snippet adaptation process takes a long time and relies on manual participation, resulting in inefficiency.

Method used

Using a method based on the interaction of agents based on large language model, automatic adaptation of code fragments is achieved by constructing prompt word modules, executor agents and consultant agents. This method uses the natural language understanding and generation capabilities of the large language model to automatically generate adapted code snippets.

Benefits of technology

Automatic adaptation of code snippets is realized, which reduces manual participation, improves adaptation efficiency, and can be widely used in different types of code snippet adaptation tasks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119536721A_ABST
    Figure CN119536721A_ABST
Patent Text Reader

Abstract

The invention relates to a code snippet automatic adaptation method and device based on large language model agent interaction. The method comprises the following steps: constructing a cue word module of a large language model according to demand information expressed by a natural language, a retrieved code snippet and a class-level context of a developer, constructing an executor agent and a consultant agent based on the large language model, the code context module serves as input to initialize an intelligent agent based on a large language model, the retrieval code snippet module and the demand refinement module are spliced to serve as input of an executor intelligent agent, and if an adapted code snippet cannot be generated, the executor intelligent agent is retrieved; if yes, the question proposed by the executor intelligent body serves as prompt input to be transmitted to the consultant intelligent body, and the answer of the consultant intelligent body to the question is inserted into the adaptation output module to serve as updating input of the executor intelligent body, so that the executor intelligent body outputs the adapted code snippets according to the updating input and the historical interaction information.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of code snippet adaptation, and in particular to a method and device for automatic adaptation of code snippets based on large language model intelligent agent interaction. Background Art

[0002] With the development of software open source communities, code snippet reuse has become an important practice in modern software development. By reusing existing software solutions, developers can avoid repeated development processes, efficiently complete software delivery and ensure software quality. These code snippets from major open source communities (such as Stack Overflow, GitHub, etc.) have been reviewed and tested by many open source contributors, so they have strong usability and reliability. However, since the code snippets in the open source community and the developer's software system have different contexts, developers need to adapt the code snippets to make them function in their own software systems and meet the corresponding software development requirements. The adaptation process is a task that takes a lot of time and effort for developers. At present, relevant studies have proposed automated adaptation methods to improve the efficiency of the adaptation process. Among them, some people analyze and solve specific types of adaptation, such as the Jigsaw tool proposed by Cottrell et al., which solves the renaming of variable identifiers by corresponding code structures. Terragni et al. conducted an empirical study on a special type of adaptation, "APIization", that is, encapsulating an independent code snippet into a format specification. They proposed four types of "API-based" models and implemented the APIZATOR tool to automatically recommend "API-based" code for reuse. Another group of researchers recommended required adaptations for developers based on historical data. Among them, Zhang et al. proposed the ExampleStack tool to recommend adaptation solutions for corresponding code snippets by analyzing the adaptation history of developers reusing code snippets from the Stack Overflow open source community in GitHub projects. Summary of the invention

[0003] Based on this, it is necessary to provide a method and device for automatic adaptation of code snippets based on large language model intelligent agent interaction to address the above technical problems.

[0004] A method for automatically adapting code snippets based on large language model agent interaction, the method comprising:

[0005] According to the requirement information expressed in natural language, the retrieved code snippets and the class-level context of the developer, a prompt word module of the large language model is constructed; the prompt word module includes: a code context module, a code snippet retrieval module, a requirement refinement module and an adaptation output module;

[0006] Build executor agents and advisor agents based on the large language model;

[0007] The code context module is used as input to initialize the agent based on the large language model, and the code snippet retrieval module and the requirement refinement module are spliced ​​as the input of the executor agent. If the adapted code snippet cannot be generated, the question raised by the executor agent is passed to the advisor agent as a prompt input, and the advisor agent's answer to the question is inserted into the adaptation output module as an updated input for the executor agent, so that the executor agent outputs the adapted code snippet according to the updated input and historical interaction information.

[0008] In one embodiment, the code context module is used to provide a prompt word including the import statement extracted from the developer code and the class to which the code snippet to be adapted belongs.

[0009] In one embodiment, the code snippet retrieval module is used to provide a prompt word containing the code snippet to be reused extracted from the code retrieval result.

[0010] In one embodiment, the requirement refinement module is used to provide prompt words including requirement refinement instructions and user requirement specifications.

[0011] In one of the embodiments, the requirement refinement module guides the consultant agent to answer questions asked by the executor agent through instructions, thereby extracting software development requirements hidden in the code.

[0012] In one of the embodiments, the adaptation output module is used to provide answers to the questions that combine the consultant agent, output instructions, and prompt words regarding adaptation dependencies.

[0013] In one of the embodiments, in the first round of interaction, the code context module is used as a prompt input of the large language model so that the intelligent agent based on the large language model has the ability to understand the code context.

[0014] In one of the embodiments, in the second round of interaction, the code snippet is generated based on the code context understanding ability of the executor agent, understanding and reasoning; if an 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 questions from the question list as prompt input to pass to the advisor 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 to obtain an updated input, and the updated input and historical interaction information are used as the executor agent input, and the adapted code snippet is output or an unrecognized successful result is fed back; the output result is inserted into the user's target context to verify the correctness of the generated code snippet.

[0016] A device for automatically adapting code snippets based on large language model agent interaction, the device comprising:

[0017] A prompt word template construction module is used to construct a prompt word module of a large language model according to the demand information expressed in natural language, the retrieved code snippets and the class-level context of the developer; the prompt word module includes: a code context module, a code snippet retrieval module, a demand refinement module and an adaptation output module;

[0018] An agent building module, used to build executor agents and advisor agents based on a large language model;

[0019] The interactive module is used to initialize the large language model-based agent using the code context module as input, and to splice the code snippet retrieval module and the requirement refinement module as the input of the executor agent. If the adapted code snippet cannot be generated, the question raised by the executor agent is passed to the advisor agent as a prompt input, and the advisor agent's answer to the question is inserted into the adaptation output module as an update input for the executor agent, so that the executor agent outputs the adapted code snippet according to the updated input and historical interaction information.

[0020] The above-mentioned method and device for automatic adaptation of code snippets based on large language model-agent interaction utilizes the task generalization ability learned by the large language model in the pre-training stage. For any adaptation task described in natural language, it can automatically generate adapted code as the output result. It is not limited to specific adaptations such as variable identifier replacement and APIization in existing methods, and has a wide range of application scenarios. The present invention does not rely on any historical data. For any code snippet in the Stack Overflow community, it can generate adapted code and recommend it to developers, and does not rely on any manual participation. Developers only need to review whether the output of the method meets the target requirements, saving a lot of labor costs. Through multi-round interactions based on agents, 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, which can improve the accuracy of the method. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1A schematic diagram of a flow chart of a method for automatically adapting code snippets based on large language model agent interaction in one embodiment;

[0022] Figure 2 A framework diagram of a method for automatically adapting code snippets based on large language model agent interaction in one embodiment;

[0023] Figure 3 A schematic diagram of a baseline method in one embodiment;

[0024] Figure 4 A schematic diagram of the structure of a device for automatically adapting code snippets based on large language model agent interaction in one embodiment. DETAILED DESCRIPTION

[0025] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0026] In one embodiment, if Figure 1 As shown, a method for automatically adapting code snippets 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 the requirement information expressed in natural language, the retrieved code snippets, and the developer's class-level context.

[0028] The prompt word module includes: a code context module, a code snippet retrieval module, a requirement refinement module and an adaptation output module.

[0029] Step 104, constructing an executor agent and an advisor agent based on the large language model.

[0030] Step 106, using the code context module as input to initialize the large language model-based agent, splicing the code snippet retrieval module and the requirement refinement module as the input of the executor agent, if the adapted code snippet cannot be generated, the question raised by the executor agent is passed to the advisor agent as a prompt input, and the advisor agent's answer to the question is inserted into the adaptation output module as an updated input for the executor agent, so that the executor agent outputs the adapted code snippet according to the updated input and historical interaction information.

[0031] The above-mentioned method for automatic adaptation of code snippets based on large language model-agent interaction utilizes the task generalization ability learned by the large language model in the pre-training stage. For any adaptation task described in natural language, the adapted code can be automatically generated as the output result. It is not limited to specific adaptations such as variable identifier replacement and APIization in existing methods, and has a wide range of application scenarios. The present invention does not rely on any historical data. For any code snippet in the Stack Overflow community, the adapted code can be generated and recommended to the developer, and does not rely on any manual participation. The developer only needs to review whether the output of the method meets the target requirements, saving a lot of labor costs. Through multiple rounds of interaction based on the agent, 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, which can improve the accuracy of the method.

[0032] The framework of this method is Figure 2 As shown in the figure, first, it builds the corresponding prompt word framework and module based on the requirements expressed in natural language, the retrieved code snippets, and the class-level context of the developer, and inputs them into the intelligent agent interaction system based on the large language model. Then, it uses the powerful natural language understanding and generation capabilities of the large language model to generate adapted code snippets and integrate them into the developer's context through a series of intelligent agent interaction processes. In this process, the operations such as requirement understanding and adaptation execution that were originally completed by the developer are all completed by the intelligent agent played by the large language model, saving the developer's labor costs. In addition, with the help of the task generalization ability obtained by the large language model in the pre-training stage, this method does not have any restrictions on the adaptation type and can be widely applied to code snippet adaptation tasks.

[0033] The natural language instructions provided by the user to the large language model are usually referred to as "prompt words", and different prompt words have a significant impact on the task performance of the large language model. The present invention designs a unique prompt word framework for the code snippet adaptation task, so that the large language model can maximize its capabilities and solve the task of automatic adaptation of code snippets. Specifically, the prompt words provided to the large language model mainly include two parts. The first part is the system prompt, which is used to initialize the large model globally; the second part is the instruction prompt, which serves as the input of the large language model. Since the large language model supports multiple rounds of interaction, the instruction prompt for each round of interaction between the user and the large language model not only includes its current input, but also includes historical interaction messages, that is, multiple groups of user message input-large language model output pairs.

[0034] In one embodiment, the code context module is used to provide a prompt word including the import statement in the extracted developer code and the class where the code snippet to be adapted is located.

[0035] Specifically, the purpose of reusing code snippets is to enable them to run normally in the developer's code context and realize their corresponding functions, so the developer's code context is crucial for the adaptation of code snippets. The present invention extracts the import statement in the developer's code, and the class to be adapted to the code snippet (including class name, class description and all methods currently implemented) constitutes a code context prompt word module. Due to the pre-training process of the large language model, the input content is often analyzed and interpreted. For example, for the provided code context, the large language model will output a series of explanations about the code function. These explanations may be able to enhance the developer's understanding of the input code, but they are not directly helpful for the automatic adaptation method of the code snippet, but will bring more token overhead to the method. Therefore, the following instruction is 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 embodiment, the code snippet retrieval module is used to provide prompt words containing the code snippets to be reused extracted from the code retrieval results.

[0037] Specifically, in the code snippet adaptation task, developers will retrieve code snippets from the Internet or open source software communities, or generate them with the help of code recommendation tools, based on the development requirements proposed by users, and then reuse these code snippets to meet the corresponding development requirements. Therefore, the present invention extracts the corresponding code snippets to be reused from the developer's search or generation results to form a retrieval code prompt word module as the adaptation object of the large language model.

[0038] In one embodiment, the requirement refinement module is used to provide prompt words including requirement refinement instructions and user requirement specifications.

[0039] Specifically, in the design of the agent interaction process, the present invention requires the agent based on the large language model to refine the requirements proposed by the user, and the prompt word module used in the process mainly includes instructions and user requirement specifications. The first half of the instruction "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 obtain the necessary information to adapt the code snippet of the above method to the class context of a certain class.") requires the agent based on the large language model to "flip" its role, from a passive question answerer to an active questioner, and express the abstract software development requirements hidden in the code in the form of natural language question answering, which is more conducive to the large language model to complete the automatic adaptation task. For the parameter setting (number of questions), a pilot experiment was conducted on 20 randomly selected adaptation tasks. It was found that if the number of questions asked by the large language model is not limited, it will cause repeated confirmation of some clear development requirements, resulting in invalid token overhead. Therefore, based on the average number of questions asked in the adaptation task of 3.2, the upper limit of the number of questions asked by the large language model in this module was determined. For the user requirement specification part of this module, the method signature, natural language requirements and input-output pairs of the code to be adapted are included to fully and accurately describe the user's development requirements.

[0040] In one embodiment, the adaptation output module provides a combination of the advisor agent's answer to the question, output instructions, and prompt words regarding adaptation dependencies.

[0041] Specifically, the adaptation output module contains two parts. The first part includes the answers to the questions raised by the large language model, and the second part includes the output instructions and dependency information about the adaptation. For the output of the large language model, there may be conflicts with the code context. The idea of ​​thinking chain is used to provide it with intermediate steps to achieve adaptation, that is, to achieve the required dependencies, to improve the accuracy of 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]. " (Translation: "The adaptation should be implemented using a certain library, a certain member variable and a certain method.") 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." (Translation: "The adaptation does not require any external libraries, member variables or methods to be implemented.")

[0042] After the above-mentioned prompt word module is established, the agent interaction process design will be carried out. The large language model shows certain learning, reasoning and decision-making capabilities, and can autonomously perceive the environment, make decisions and execute corresponding behaviors as an independent agent. Due to the complexity of the code snippet adaptation task, the iterative demand acquisition process between users and software developers in software engineering is referenced. Based on the large language model, the corresponding agent is constructed to play a specific role, and the task is completed through collaboration between agents. The present invention constructs two different agents based on the large language model. The first is the executor agent, and its main responsibilities include: 1) If the large language model perceives that there is information missing or confusion in the user's requirements, it generates corresponding questions to refine and confirm the user's requirements; 2) If the large language model believes that the current information is sufficient for execution adaptation, the adapted code snippet is generated according to the user's explanation of the user's requirements and questions. The second is the consultant agent, whose main responsibility is to respond to questions raised by the executor agent.

[0043] In one of the embodiments, since the adaptation task contains complex elements, constructing all the prompt word modules as a single input and providing it to the large language model will cause the input message to be too long, thereby causing the large language model to have a reduced accuracy in understanding the input. Therefore, the task to be performed by the agent is decomposed, and the task description is provided to the large language model inside it through multiple rounds of instruction input. In the first round of interaction, the code context module is used as a prompt input for the agent based on the large language model, so that the agent has the ability to understand the code context, thereby reducing the difficulty of the large language model inside it to understand the super-long text context. Since the historical interaction message is used as input in each round of interaction with the agent, it is also guaranteed 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 executor agent generates a code snippet based on its code context understanding ability, understanding and reasoning; if an 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 questions from the question list as prompt input to be passed to the advisor agent.

[0045] Specifically, in the second round of interaction, the code snippet retrieval module and the requirement refinement module are spliced ​​and provided to the large language model as prompt input, requiring the executor agent to use the "reflection" ability to understand and reason based on the previous code context, the retrieved code snippets, and the requirements to be implemented, and determine whether the adaptation can be completed. If so, the adapted code snippet is directly generated, and the code snippet is identified 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 and passed to the consultant agent as prompt input, requiring it to answer these questions based on the code context and output them.

[0046] In another embodiment, in the third interaction, the consultant agent's answer to the question is inserted into the adaptation output module to obtain an updated input, and the updated input and historical interaction information are used as the executor agent input, and the adapted code snippet is output or an unrecognized successful result is fed back; the output result is inserted into the user's target context to verify the correctness of the generated code snippet.

[0047] Specifically, the adapted output module provides the large language model of the executor agent as input, requiring it to output the adapted code snippet according to the answer of the consultant agent based on the previous interaction history. Similar to the processing of the output results of the executor agent in the second round of interaction, the adapted code snippet in its output will be identified. If the identification is not successful, an empty string will be returned as the result.

[0048] The generation of the code snippet is now completed. The beneficial effects of the present invention are described below through experiments.

[0049] The ClassEval benchmark was used to evaluate the adaptation performance of the present invention. ClassEval is a class-level code generation benchmark that contains 100 Python classes and their associated test suites. Because it contains class-level code, it can effectively verify the adaptation of code snippets to such context-dependent tasks. In addition, ClassEval is an artificially constructed dataset covering a variety of topics that simulates real software development scenarios. This dataset was released in 2023, so it can reduce the problem of data leakage in large language model training. Based on ClassEval, the class-level context is obtained by excluding the target method and all its 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 were obtained.

[0050] Three widely studied large language models were selected, including a general-purpose large language model GPT-3.5 and a code large language model CodeLlama after instruction fine-tuning. For GPT-3.5, the model of the gpt-3.5-turbo-0613 checkpoint was accessed through the OpenAI API. For CodeLlama, its 34B (with about 34 billion parameters) version was selected.

[0051] Since there is no universal method for automatic adaptation of code snippets, a prompt method based on a large language model is designed as a baseline. The prompts used are as follows: Figure 3 Shown in the upper part.

[0052] In addition, in order to explore the effectiveness of the agent interaction in the present invention, only the executor agent is used to generate the adapted code through multiple rounds of interaction. The prompt process is as follows: Figure 3 Shown in the lower part.

[0053] Pass@k is used as the main indicator to evaluate the correctness of large language model adaptation. It measures the possibility 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 passed the test. Considering the generation cost, n is set to 5. Specifically, pass@1 and pass@5 are calculated for each adaptation task, reflecting the accuracy and actual solution discovery efficiency, that is, whether a solution can be found by browsing no more than 5 code snippets adapted by the large language model.

[0056] In addition, CodeBLEU is used as a supplementary evaluation metric. It compares the grammatical, syntactic and semantic similarity between the adapted output and the correct result through n-gram matching, abstract syntax tree and data flow. It is used to evaluate the similarity between the generated fragments and the standard solution.

[0057] In order to mitigate the impact of the randomness of the output of the large language model, its output is sampled according to the kernel sampling method. Specifically, for each task, 5 output results are randomly generated and collected for evaluation. The experiment also explored the impact of the temperature setting of the large language model on its performance. The optimal temperature was determined by grid search. The temperature ranged from 0 to 1 with an interval of 0.2. The optimal temperature of GPT-3.5 was 0.8 and the optimal temperature of CodeLlama was 0.8. The maximum token window of all large language models was set to 2,048 to ensure consistent context length. All experiments were performed on two GeForceRTX 4090-24G GPUs.

[0058] Table 1 Comparison of the performance of the large language model-based agent interaction method and the baseline method on the adaptation task

[0059]

[0060] Table 1 shows the effectiveness of the present invention compared to the baseline method. For the GPT-3.5 model, the large language model-based agent interaction method achieved the best results in both pass@1 and pass@5 indicators, which were 71.80 and 82.50 respectively. This means that the present invention has an accuracy of over 70% in 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 invention has an improvement of 21.80%, 26.00% and 12.51% in pass@1, pass@5 and CodeBLEU indicators respectively. Compared with the GPT-3.5-SingleAgent method with multiple rounds of interaction with a single agent, the present invention has an improvement of 3.3% and 9% in pass@1 and pass@5, although there is a slight decrease in the CodeBLEU indicator. The reason may be that the present invention takes into account the advice of the advisor 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 achieved the best results in all three evaluation indicators, which were 45.95, 67.32 and 45.58 respectively, and achieved at least 8% and 2% improvement compared with the Base and SingleAgent methods respectively.

[0061] In summary, the problem of code snippet adaptation in the field of software engineering is a task that requires a lot of manpower to complete. In the present invention, an intelligent agent interaction method based on a large language model is used to solve the problem of automatic adaptation of code snippets. Specifically, a prompt framework and module are designed for this task, and prompt words can be automatically generated for each adaptation task as the input of the large language model. In addition, the large language model's powerful natural language and code understanding and generation capabilities are utilized to allow it to play different intelligent agent roles, and human developers are imitated to solve the code snippet adaptation problem through multiple rounds of interaction processes to generate adapted code snippets. Finally, experiments were conducted on the ClassEval benchmark and two large models to verify the effectiveness of the present invention compared to the simple prompt word method.

[0062] In one embodiment, if Figure 4 As shown, a device for automatically adapting code snippets based on large language model and intelligent agent interaction is provided, comprising: a prompt word template construction module 402, an intelligent agent construction module 404 and an interaction module 406.

[0063] The prompt word template construction module 402 is used to construct a prompt word module of a large language model according to the requirement information expressed in natural language, the retrieved code snippets and the class-level context of the developer; the prompt word module includes: a code context module, a code snippet retrieval module, a requirement refinement module and an adaptation output module;

[0064] An agent building module 404, used to build an executive agent and an advisor agent based on a large language model;

[0065] The interaction module 406 is used to initialize the large language model-based agent using the code context module as input, and to splice the code snippet retrieval module and the requirement refinement module as the input of the executor agent. If the adapted code snippet cannot be generated, the question raised by the executor agent is passed to the advisor agent as a prompt input, and the advisor agent's answer to the question is inserted into the adaptation output module as an update input for 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 may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0067] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.

Claims

1. A method for automatic adaptation of code snippets based on large language model agent interaction, characterized in that: The method comprises: According to the requirement information expressed in natural language, the retrieved code snippets and the class-level context of the developer, a prompt word module of the large language model is constructed; the prompt word module includes: a code context module, a code snippet retrieval module, a requirement refinement module and an adaptation output module; Build executor agents and advisor agents based on the large language model; The code context module is used as input to initialize the agent based on the large language model, and the code snippet retrieval module and the requirement refinement module are spliced ​​as the input of the executor agent. If the adapted code snippet cannot be generated, the question raised by the executor agent is passed to the advisor agent as a prompt input, and the advisor agent's answer to the question is inserted into the adaptation output module as an updated input for the executor agent, so that the executor agent outputs the adapted code snippet according to the updated input and historical interaction information.

2. The method according to claim 1, characterized in that The code context module is used to provide a prompt word of the class containing the import statement extracted from the developer code and the code snippet to be adapted.

3. The method according to claim 1, characterized in that The code snippet retrieval module is used to provide prompt words containing the code snippets to be reused extracted from the code retrieval results.

4. The method according to claim 1, characterized in that: The requirement refinement module is used to provide prompt words including requirement refinement instructions and user requirement specifications.

5. The method according to claim 4, characterized in that The requirement refinement module guides the consultant agent to answer the questions of the executor agent through instructions, and extracts the software development requirements hidden in the code.

6. The method according to claim 1, characterized in that The adaptation output module is used to provide answers to the questions combined with the consultant agent, output instructions and prompt words about the adaptation dependencies.

7. The method according to any one of claims 1 to 6, characterized in that: Initialize the large language model-based agent using the code context module as input, including: In the first round of interaction, the code context module is used as a prompt input to the large language model-based agent to enable it to have the ability to understand code context.

8. The method according to claim 7, characterized in that The code snippet retrieval module and the requirement refinement module are spliced ​​as the input of the executor agent. If the adapted code snippet cannot be generated, the question raised by the executor agent is passed as a prompt input to the consultant agent, including: In the second round of interaction, the code snippets are generated based on the code context understanding ability of the actor agent, and the code snippets are understood and reasoned; If an adapted code snippet is generated, the interaction is terminated and the adapted code snippet is output; If not, the executive agent generates a question list and selects questions from the question list to pass to the advisor agent as prompt input.

9. The method according to claim 8, characterized in that Inserting the consultant agent's answer to the question into the adaptation output module as an update input of the executor agent, so that the executor agent outputs an adapted code snippet according to the update input and historical interaction information, including: In the third interaction, the consultant agent's answer to the question is inserted into the adaptation output module to obtain an updated input, and the updated input and historical interaction information are used as the executor agent input, and the adapted code snippet is output or an unrecognized successful result is fed back; The output is inserted into the user's target context to verify the correctness of the generated code snippet.

10. A code snippet automatic adaptation device based on large language model intelligent agent interaction, characterized in that: The device comprises: A prompt word template construction module is used to construct a prompt word module of a large language model according to the demand information expressed in natural language, the retrieved code snippets and the class-level context of the developer; the prompt word module includes: a code context module, a code snippet retrieval module, a demand refinement module and an adaptation output module; An agent building module, used to build executor agents and advisor agents based on a large language model; The interactive module is used to initialize the large language model-based agent using the code context module as input, and to splice the code snippet retrieval module and the requirement refinement module as the input of the executor agent. If the adapted code snippet cannot be generated, the question raised by the executor agent is passed to the advisor agent as a prompt input, and the advisor agent's answer to the question is inserted into the adaptation output module as an update input for the executor agent, so that the executor agent outputs the adapted code snippet according to the updated input and historical interaction information.

Citation Information

Patent Citations

  • Code generation and defect repair method and device

    CN116909532A

  • 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

  • RPA process execution code generation method and device based on AI and AI Agent

    CN118444894A

  • Intelligent question and answer method and device, computer equipment and program product

    CN118820436A

Cited By

  • Adjustable large language model active recommendation agent system

    CN120781988A