A step-by-step code generation method based on large language model of code forest agent
By introducing code forest Agent and tree structure management methods in the large language model (LLM), the problem that LLM cannot correctly associate the previous and subsequent codes when generating code in step by step is solved, and the generation of syntax and logical complete code is achieved.
Patent Information
- Application Number
- CN202411797657.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-09
- Publication Date
- 2025-04-01
AI Technical Summary
When generating code in step by step, the big language model (LLM) cannot correctly associate the previous and subsequent codes, resulting in the generated code being syntax and logic incomplete and unable to run.
Using a code forest Agent-based method, LLM is used to generate code in step by step through multiple tree structures. The specific steps include building a code forest based on the initial generated code generation, Agent automatically manages the code forest, obtains code snippets and identifier information, and appending the generated code to the code forest.
Ensure that the final generated code is syntax and logically complete and correct, avoiding the problem of LLM's self-inference of unreliable available identifiers, and improving the accuracy and operability of generated code.
Smart Images

Figure CN119739372A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to a method for generating codes in steps using a large language model based on a code forest agent, and belongs to the technical field of data processing. Background Art
[0002] Currently, using a large language model (LLM) to generate code is a common solution. Due to the limitations of LLM's training data and the maximum number of tokens generated each time (max_tokens), code-related training data usually does not use very long code snippets, so the model capacity and max_tokens usually do not allow the generation of very long content at one time. LLM cannot generate long and complex code in multiple files through a single conversation.
[0003] The first solution that usually comes to mind is to generate code in steps. However, the codes generated in steps are often incoherent and cannot be correctly associated with each other, resulting in the final generated code being wrong and unrunnable. The most common error is the identifier (variable) problem. Take Python code as an example, for example:
[0004] Case 1: Is the variable a global variable or a local variable? Can the variable be used in the current generated code?
[0005] Assume that there is the following generated code. The subsequent generated code can only use the global variable a1, but cannot use the local variable a2 in the func function. a2 can only be used in the func function. For relatively simple or short codes and prompt words, LLM can handle this situation better because such situations are also included in the training data. However, in complex or long codes and prompt words, LLM may not be able to handle this situation correctly and use the variable a2 incorrectly.
[0006] Generated code:
[0007] a1=1#global variable
[0008] def func():
[0009] a2=2#Local variables in func function
[0010] The following generated code:
[0011] print(a1)# can output the result correctly
[0012] print(a2) #Error, error message: variable a2 is not defined
[0013] Case 2: Whether the variable can be referenced in the code logic;
[0014] Because LLM can only understand that the variable is before the code to be generated, but cannot understand the logical structure, it may mistakenly think that the variable has been defined and can be used, resulting in incorrect reference of the variable. In the following example, print(b) may be output when variable b is not defined, resulting in an error.
[0015] Generated code:
[0016] if a=0:
[0017] b=1
[0018] The following generated code:
[0019] else:
[0020] print(b)
[0021] Case 3. The variable is ignored in the context;
[0022] When the LLM dialog context continues to increase, the variables in the generated code may be ignored, so that the subsequent generation fails to notice that the variable already exists and is repeatedly defined, or a similar but wrong variable is fabricated, which eventually causes the program to report an error.
[0023] For example, in the generated code, a1 has been changed to 2, but the subsequent generated code redefines a variable a1=1, overwriting the previous a1=2; or a variable b1 is fabricated, but the intention is to output variable b. Generated code:
[0024] a1=1
[0025] a2=2
[0026] a1=2
[0027] b=3
[0028] The following generated code:
[0029] a1=1
[0030] print(b1)
[0031] Of course, the incoherence of the previous and subsequent codes that may occur during step-by-step generation is not only caused by variables, but also by functions and other identifiers. I will not list them one by one here, but I will take variables as an example. However, most of them are errors caused by LLM's inability to understand the logical structure of the code or too much context being ignored. Therefore, if you only rely on LLM's own ability to generate code step by step, the final code will most likely not run at all, so you need to provide some clear auxiliary information to help LLM better generate correct code.
[0032] Then how to integrate the step-by-step generated codes is the first problem to be solved in implementing step-by-step code generation. Specifically, how to correctly associate the subsequently generated codes with the previously generated codes, that is, how to ensure that the final code is complete and correct in terms of syntax and logic. Summary of the invention
[0033] Purpose: In view of at least one of the above technical problems, the present application provides a method for generating code step by step from a large language model based on a code forest agent, which manages the LLM step by step code generation through multiple tree structures, effectively ensuring that the final generated code is grammatically and logically complete and correct.
[0034] Technical solution: To solve the above technical problems, the technical solution adopted in this application is:
[0035] In a first aspect, the present application provides a method for step-by-step code generation based on a large language model of a code forest agent, the method comprising:
[0036] S1. Generate and construct a code forest based on the initially generated code; wherein the code forest includes multiple code trees, one code tree represents all code contents in a code file, and organizes nodes, i.e., organizes code snippets, through a tree structure; each node of the code tree contains a code snippet and code snippet identifier information;
[0037] S2,Agent automatically manages the code forest, obtains code snippets and code snippet identifier information when generating code, and then appends the generated code to the code forest.
[0038] In some embodiments, S1, constructing a code forest according to the initially generated code generation includes:
[0039] S11, obtaining the initial code snippet generated by the large language model according to the requirements;
[0040] S12, parsing the initial code snippet to obtain initial code snippet identifier information; wherein the identifier includes at least one of a subsequently available variable, function, constant, object, class name, and module;
[0041] S13, constructing a root node of a code tree according to the initial code fragment and the corresponding initial code fragment identifier information;
[0042] S14, parsing the code snippet written after the initial code snippet to obtain the corresponding code snippet identifier information; forming a new node according to the code snippet and the corresponding code snippet identifier information, and appending it to the code tree according to the code structure, repeating this step, and finally forming a code tree with the same code structure, and writing the finally generated code into the code file;
[0043] S15. Generate multiple code trees according to steps S11 to S14, and the multiple code trees form a code forest.
[0044] In some embodiments, S2, the Agent automatically manages the code forest, obtains code snippets and code snippet identifier information when generating code, and then appends the generated code to the code forest, including:
[0045] S21. When generating new code, the Agent first uses the tool to obtain the required code tree from the code forest according to the requirements of the generated code, and locates the node position of the code tree to add the node;
[0046] S22, then starting from the node, find all the ancestor nodes of the node, all the identifier information of these ancestor nodes is all the identifier information that can be used by the code generated by the node to be added, and then attach these identifier information when the large language model LLM generates the code, and clearly indicate that these are all the identifier information that can be used by the currently generated code;
[0047] S23. Obtain the code snippet generated by the large language model according to the requirements and these identifier information, parse the newly generated code snippet to obtain the corresponding code snippet identifier information; construct a new node according to the code snippet and the corresponding code snippet identifier information, and append the node to the code tree according to the located added node position.
[0048] In some embodiments, a code forest is represented as a working directory, which is composed of all code files under the working directory.
[0049] In some embodiments, the generated code snippet is parsed by a parsing program to obtain the corresponding code snippet identifier information, or parsed using a large language model (LLM).
[0050] In some embodiments, the code forest is in a JSON structured form, including: tree names of each code tree, previous nodes and next nodes of all nodes, code snippets of each node, and corresponding code snippet identifier information.
[0051] Furthermore, the code snippet identifier information includes an identifier name, an identifier value, and an identifier description information.
[0052] In some embodiments, the Agent automatically manages the code forest, including: first, the Agent uses the get_tree tool to specify the tree name of the code tree to be obtained according to the demand, the get_tree tool searches the field name in JSON according to the tree name, obtains the code tree and returns it to the Agent, the Agent determines to which node position the code generated by the current demand should be added, uses the locate_node tool to specify the position to be inserted, the locate_node tool recursively searches the previous node pre_node field in JSON according to the specified position, obtains the identifier information of all ancestor nodes until the root node of the tree, returns the identifier information of all the found ancestor nodes to the Agent, the Agent sends the demand and identifier information to the LLM to start generating code, the generated code is tested to be correct, and then the newly generated code snippet is parsed to obtain the corresponding code snippet identifier information; a new node is constructed according to the code snippet and the corresponding code snippet identifier information, and the node is appended to this code tree according to the located added node position.
[0053] In a second aspect, the present application provides a device for step-by-step code generation based on a large language model of a code forest agent, the device comprising:
[0054] A code forest construction module is used to: generate and construct a code forest according to the initially generated code; wherein the code forest includes multiple code trees, one code tree represents all the code content in a code file, and organizes nodes through a tree structure, that is, organizes code fragments; each node of the code tree contains a code fragment and a code fragment identifier information;
[0055] The code forest management module is used to: the Agent automatically manages the code forest, obtains code snippets and code snippet identifier information when generating code, and then appends the generated code to the code forest.
[0056] In a third aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the steps of the method described in the first aspect.
[0057] In a fourth aspect, the present application provides a computer device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the method described in the first aspect when executing the computer program.
[0058] In a fifth aspect, the present application provides a computer program product, comprising a computer program, which implements the steps of the method described in the first aspect when executed by a processor.
[0059] Beneficial effects: The method and device for step-by-step code generation based on a large language model of a code forest agent provided in the present application have the following advantages: In the present application, by adopting code parsing to extract identifiers (variables), the problem of situation 1 in the background technology is solved, and during the parsing process, it has been distinguished which identifiers (variables) in the code fragment can be used for subsequent code generation. Locate and find all ancestor nodes of the node, solve the problem of situation 2 in the background technology, and obtain all identifier (variable) information that can be used to generate code from the current position according to the structure of the code tree. When generating code, the information of all available identifiers (variables) obtained above is clearly prompted, which solves the problem of situation 3 in the background technology, and does not require LLM to infer unreliable available identifiers (variables) by itself. By introducing code forests and automatic management agents, it is possible to effectively solve the problem that when LLM generates code step by step, the generated codes are incoherent and cannot be correctly associated with each other, resulting in the final generated code being wrong and unoperable. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] Figure 1 A schematic diagram of an application scenario of a method according to an embodiment of the present application;
[0061] Figure 2 A schematic diagram of a method for generating codes step by step using a large language model based on a code forest agent according to an embodiment of the present application;
[0062] Figure 3 A schematic diagram of a device for generating codes in steps using a large language model based on a code forest agent according to an embodiment of the present application;
[0063] Figure 4 A schematic diagram of an electronic device provided according to an embodiment of the present application;
[0064] Figure 5 This is a schematic diagram of an example of a code forest in an embodiment. DETAILED DESCRIPTION
[0065] The present application is further described below in conjunction with the accompanying drawings and embodiments. The following embodiments are only used to more clearly illustrate the technical solution of the present application, and cannot be used to limit the protection scope of the present application.
[0066] In this application, the professional terms are explained as follows:
[0067] Large Language Model (LLM): A deep learning model that uses a large amount of text data for training, has billions of parameters, and can understand and process natural language. It is a general-purpose natural language processing (NLP) model.
[0068] Prompt words: text content used to prompt LLM to give targeted answers, usually including system prompts, which are used to prompt LLM to generate answers in the current context window in what role, scenario, and format. User prompts generally refer to the conversation content entered by the user, which are used to prompt LLM to complete tasks, including but not limited to daily conversations, answering questions, translation, writing, and coding.
[0069] Context: LLM itself does not record the content of the conversation. The context is used to record all the current conversation content, including: prompt words, and the answer content generated by LLM. LLM infers and generates content based on the current context content. Of course, the context window size is limited. Currently, taking gpt-4o as an example, the context window size can reach 128k tokens.
[0070] Tool: This is implemented through the function_calling function of LLM. By providing the tool name and tool description, LLM calls the tool by replying with formatted JSON, executes the tool in the external operating environment, and returns the operating results to LLM. Agents can perceive external information or take actions through tools.
[0071] Before describing the embodiments of the present application in detail, a specific scenario example is used to reveal the scenario in which the technical solution of the present application can be used. In a possible scenario example, specifically, Figure 1 As shown, the user performs specific operations on the client 110, and the client 110 is deployed with a data collection device and a large language model (LLM). The data collection device is used to collect user requirements on the client 110, and the collected user requirements are transmitted to the server through the network. The server uses the large language model (LLM) to generate code according to the requirements and available identifier information through a preset program, parse the code, generate nodes, and form a code tree. Multiple code trees form a code forest; the Agent automatically manages the code forest, obtains code fragments and code fragment identifier information when generating code, and then appends the generated code to the code forest. In the above manner, multiple tree structures are used to manage LLM to generate code step by step, effectively ensuring that the final generated grammatically and logically complete and correct code is generated.
[0072] The large language model (LLM) can run on the client terminal side or the server side. The method of the present application can be configured in the large language model (LLM) on the client terminal / or server side, and the client terminal and the server are connected through network transmission to realize multi-terminal interaction.
[0073] The server may be an electronic device with certain computing and processing capabilities. For example, the server may be a server of a distributed system, or a system with multiple processors, memories, network communication modules, etc. operating in collaboration. The server may also be a cloud server, or an intelligent cloud computing server or intelligent cloud host with artificial intelligence technology. The server may also be a server cluster formed by several servers. Alternatively, with the development of science and technology, the server may also be a new technical means capable of realizing the corresponding functions of the embodiments of the specification. For example, it may be a new form of "server" based on quantum computing.
[0074] The client terminal may be an electronic device with network access capability. Specifically, for example, the terminal may be a desktop computer, a tablet computer, a laptop computer, a smart phone, etc. Alternatively, the terminal may also be software that can run in the electronic device.
[0075] The network may be any type of network that can support data communications using any of a variety of available protocols, including but not limited to TCP / IP, SNA, IPX, etc. The one or more networks may be a local area network (LAN), an Ethernet-based network, a token ring, a wide area network (WAN), the Internet, a virtual network, a virtual private network (VPN), an intranet, an extranet, a public switched telephone network (PSTN), an infrared network, a wireless network (e.g., Bluetooth, WIFI), and / or any combination of these and / or other networks.
[0076] Example 1: Figure 2 As shown, this embodiment provides a method for generating code step by step from a large language model based on a code forest agent, which can be applied to a large language model (LLM), and the large language model (LLM) can run in a client terminal. The method for generating code step by step from a large language model based on a code forest agent includes:
[0077] Step S1, constructing a code forest based on the initially generated code; wherein the code forest includes one or more code trees, a code tree represents all code contents in a code file, and organizes nodes through a tree structure, that is, organizes code fragments; each node of the code tree contains a code fragment and a code fragment identifier information.
[0078] It should be noted that no reference code is needed during the initial generation, and there is no code reference, so the code forest is generated from scratch. Each node of the code tree is composed of a code snippet and its identifier information. The identifier information can be any code-related information such as variables, constants, functions, objects, class names, modules, etc. that can be used later. A code tree represents all the code content in a code file, and organizes nodes through a tree structure, that is, organizes code snippets. A code forest is composed of multiple code trees, which can be understood as a code forest composed of all code files in a working directory.
[0079] In this embodiment, the identifier takes variables as an example, and the preliminary construction process of the code forest is as follows: First, each node of the tree includes: the generated code snippet, all the variables that can be used subsequently, and there is no unique method to parse all the variables that can be used subsequently from the generated code. The generated code snippet can be parsed by a parsing program, or it can be parsed by LLM. In general, program parsing is more accurate, but the flexibility is not so high, and the results of LLM parsing are less reliable, but more flexible. In short, choose a suitable method to parse the code snippet to extract all the variables that can be used subsequently, forming a node containing the code snippet and all the variables that can be used subsequently as the root of a tree. If you write code after this code, the newly generated code snippet and its variables constitute a new node, which is appended to the tree according to the code structure, and so on, and eventually a tree with the same code structure will be formed, and the code will be written into the code file. If you want to generate code for another requirement, then generate another tree according to the above process.
[0080] In general, a working directory is a code forest. A code forest is maintained in a one-to-one correspondence with a working directory. The code forest is a logical abstraction of the working directory, which is used to assist LLM in understanding the code structure and clearly highlight and emphasize the relevant information of the code by means of code parsing. The working directory is the physical entity that stores the generated code. Figure 5 shown.
[0081] In some embodiments, step S1 may specifically include:
[0082] S11, obtaining the initial code snippet generated by the large language model according to the requirements;
[0083] S12, parsing the initial code snippet to obtain the initial code snippet identifier information; wherein the identifier information includes at least one of a subsequently available variable, function, constant, object, class name, and module;
[0084] S13, constructing a root node of a code tree according to the initial code fragment and the corresponding initial code fragment identifier information;
[0085] S14, parsing the code snippet written after the initial code snippet to obtain the corresponding code snippet identifier information; forming a new node according to the code snippet and the corresponding code snippet identifier information, and appending it to the code tree according to the code structure, repeating this step, and finally forming a code tree with the same code structure, and writing the finally generated code into the code file;
[0086] S15. Generate multiple code trees according to steps S11 to S14, and the multiple code trees form a code forest.
[0087] In some embodiments, the generated code snippet is parsed by a parser, or by a large language model (LLM). Examples of the parser include: ast module in the python standard library is used to parse python code; python's tree-sitter library can parse most languages; JavaParser can be used to parse java code; ANTLR supports syntax analysis tools for multiple languages.
[0088] Step S2: The Agent automatically manages the code forest, obtains code snippets and code snippet identifier information when generating code, and then appends the generated code to the code forest.
[0089] Step S1 describes how the code forest is formed and what its structure is. Step S2 mainly describes how the Agent manages the code forest and how to use the code forest to assist LLM in better code generation.
[0090] In some embodiments, step S2 may specifically include:
[0091] S21. When generating new code, the Agent first uses the tool to obtain the required code tree from the code forest according to the requirements of the generated code, and locates the node position of the code tree to add the node;
[0092] S22, then starting from the node, find all the ancestor nodes of the node, all the identifier information of these ancestor nodes is all the identifier information that can be used by the code generated by the node to be added, and then attach these identifier information when the large language model LLM generates the code, and clearly indicate that these are all the identifier information that can be used by the currently generated code;
[0093] S23. Obtain the code snippet generated by the large language model according to the requirements and these identifier information, parse the newly generated code snippet to obtain the corresponding code snippet identifier information; construct a new node according to the code snippet and the corresponding code snippet identifier information, and append the node to the code tree according to the located added node position.
[0094] In some embodiments, the code forest is in a JSON structured form, including: tree names of each code tree, previous nodes and next nodes of all nodes, code snippets of each node, and corresponding code snippet identifier information.
[0095] The code snippet identifier information includes an identifier name, an identifier value, and an identifier description information.
[0096] For example, we use JSON to represent the structure of the code forest. Of course, it can also be represented in other forms, or it can be a class in the code. Here, JSON is used for the sake of convenience. The structure of the code forest is not unique. The following is an example of a code forest:
[0097]
[0098] Combined with the above structure of representing the code forest using JSON as an example, the process of Agent automatically managing the code forest is further described, including: first, the Agent uses the get_tree tool to specify the tree name of the code tree to be obtained according to the demand, the get_tree tool searches the field name in JSON according to the tree name, obtains the code tree and returns it to the Agent, the Agent determines to which node position the code generated by the current demand should be added, and uses the locate_node tool to specify the position to be inserted. The locate_node tool recursively searches the previous node pre_node field in JSON according to the specified position, obtains the identifier information of all ancestor nodes until the root node of the tree, and returns the identifier information of all found ancestor nodes to the Agent, the Agent sends the demand and identifier information to the LLM to start generating code, the generated code is tested to be correct, and then the newly generated code fragment is parsed to obtain the corresponding code fragment identifier information; a new node is formed according to the code fragment and the corresponding code fragment identifier information, and the node is appended to this code tree according to the located added node position.
[0099] In summary, the method for generating code in steps using a large language model based on a code forest agent provided in the above-mentioned embodiment solves the problem of situation 1 in the background technology by extracting identifiers (variables) by parsing the code. In the parsing process, it has been distinguished which identifiers (variables) in the code fragment can be used for subsequent code generation. All ancestor nodes of the node are located and found, which solves the problem of situation 2 in the background technology. All identifiers (variables) that can be used to generate code from the current position are obtained according to the structure of the code tree. When generating code, the information of all available identifiers (variables) obtained above is clearly prompted, which solves the problem of situation 3 in the background technology, and LLM does not need to infer unreliable available identifiers (variables) by itself. By introducing a code forest and an automatic management agent, the problem that when LLM generates code step by step, the generated codes are incoherent and cannot be correctly associated with each other, resulting in the final generated code being wrong and unoperable can be effectively solved.
[0100] Example 2: Based on Example 1, Figure 3 As shown, the present application provides a device for generating codes step by step based on a large language model of a code forest agent, the device comprising:
[0101] A code forest construction module is used to: generate and construct a code forest according to the initially generated code; wherein the code forest includes multiple code trees, one code tree represents all the code content in a code file, and organizes nodes through a tree structure, that is, organizes code fragments; each node of the code tree contains a code fragment and a code fragment identifier information;
[0102] The code forest management module is used to: the Agent automatically manages the code forest, obtains code snippets and code snippet identifier information when generating code, and then appends the generated code to the code forest.
[0103] Embodiment 3: Based on Embodiment 1, this embodiment provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the method described in Embodiment 1 are implemented.
[0104] Embodiment 4: Based on embodiment 1, as Figure 4 As shown, this embodiment provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the method described in Embodiment 1 when executing the computer program.
[0105] Embodiment 5: Based on Embodiment 1, this embodiment proposes a hardware system that applies the method for generating codes in steps using a large language model based on a code forest agent in Embodiment 1. The method is applied to the server side.
[0106] The terminal may be an electronic device with network access capability, for example, a desktop computer, a tablet computer, a laptop computer, a smart phone, a digital assistant, a shopping guide terminal, a television, etc.
[0107] The server can be an electronic device with certain computing and processing capabilities. For example, the server can be a server of a distributed system, or a system with multiple processors, memories, network communication modules, etc. operating in collaboration. The server can also be a cloud server, or an intelligent cloud computing server or intelligent cloud host with artificial intelligence technology. The server can also be a server cluster formed by several servers. Alternatively, with the development of science and technology, the server can also be a new technical means that can realize the corresponding functions of the implementation method of the specification. For example, it can be a new form of "server" based on quantum computing.
[0108] Embodiment 6:
[0109] Based on Example 1, this embodiment also provides a computer program product including instructions, which, when executed by a computer, enables the computer to execute a method for generating codes step by step from a large language model based on a code forest agent in Example 1.
[0110] It should be understood that the specific examples in this article are only intended to help those skilled in the art better understand the implementation methods of this specification, rather than limiting the scope of this application.
[0111] It can be understood that in the various implementations of this specification, the size of the sequence number of each process does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the implementation methods of this specification.
[0112] It can be understood that the various embodiments described in this specification can be implemented individually or in combination, and the embodiments of this specification are not limited to this.
[0113] Unless otherwise specified, all technical and scientific terms used in the embodiments of this specification have the same meaning as those generally understood by those skilled in the art of the technical field of this specification. The terms used in this specification are only for the purpose of describing specific embodiments and are not intended to limit the scope of this specification. The term "and / or" used in this specification includes any and all combinations of one or more related listed items. The singular forms of "a", "above", and "the" used in the embodiments of this specification and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings.
[0114] It can be understood that the processor of the embodiment of this specification can be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method implementation can be completed by the hardware integrated logic circuit or software instructions in the processor. The above processor can be a general processor, a digital signal processor (Digital Signal Processor, DSP), an application-specific integrated circuit (Application Specific Integrated Circuit, ASIC), a field programmable gate array (Field Programmable Gate Array, FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiment of this specification can be implemented or executed. The general processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in the embodiment of this specification can be directly embodied as a hardware decoding processor to perform, or the hardware and software modules in the decoding processor are combined and executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory, and the processor reads the information in the memory and completes the steps of the above method in combination with its hardware.
[0115] It is understood that the memory in the embodiments of this specification may be a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memory. Among them, the non-volatile memory may be a read-only memory (ROM), a programmable read-only memory (programmable ROM, PROM), an erasable programmable read-only memory (erasablePROM, EPROM), an electrically erasable programmable read-only memory (EEPROM) or a flash memory. The volatile memory may be a random access memory (RAM). It should be noted that the memory of the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0116] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this specification.
[0117] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method implementation methods and will not be repeated here.
[0118] In the several embodiments provided in this specification, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device implementation described above is only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0119] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0120] In addition, each functional unit in each embodiment of the present specification may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0121] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this specification, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the method described in each embodiment of this specification. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc., various media that can store program codes.
[0122] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the technical principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A method for generating code step by step from a large language model based on a code forest agent, characterized in that: The method comprises: S1. Generate and construct a code forest based on the initially generated code; wherein the code forest includes multiple code trees, one code tree represents all code contents in a code file, and organizes nodes, i.e., organizes code snippets, through a tree structure; each node of the code tree contains a code snippet and code snippet identifier information; S2,Agent automatically manages the code forest, obtains code snippets and code snippet identifier information when generating code, and then appends the generated code to the code forest.
2. The method for generating code step by step from a large language model based on a code forest agent according to claim 1 is characterized in that: S1. Generate and build a code forest based on the initially generated code, including: S11, obtaining the initial code snippet generated by the large language model according to the requirements; S12, parsing the initial code snippet to obtain initial code snippet identifier information; wherein the identifier includes at least one of a subsequently available variable, function, constant, object, class name, and module; S13, constructing a root node of a code tree according to the initial code fragment and the corresponding initial code fragment identifier information; S14, parsing the code snippet written after the initial code snippet to obtain the corresponding code snippet identifier information; forming a new node according to the code snippet and the corresponding code snippet identifier information, and appending it to the code tree according to the code structure, repeating this step, and finally forming a code tree with the same code structure, and writing the finally generated code into the code file; S15. Generate multiple code trees according to steps S11 to S14, and the multiple code trees form a code forest.
3. The method for generating code step by step from a large language model based on a code forest agent according to claim 1, characterized in that: S2, Agent automatically manages the code forest, obtains code snippets and code snippet identifier information when generating code, and then appends the generated code to the code forest, including: S21. When generating new code, the Agent first uses the tool to obtain the required code tree from the code forest according to the requirements of the generated code, and locates the node position of the code tree to add the node; S22, then starting from the node, find all the ancestor nodes of the node, all the identifier information of these ancestor nodes is all the identifier information that can be used by the code generated by the node to be added, and then attach these identifier information when the large language model LLM generates the code, and clearly indicate that these are all the identifier information that can be used by the currently generated code; S23. Obtain the code snippet generated by the large language model according to the requirements and these identifier information, parse the newly generated code snippet to obtain the corresponding code snippet identifier information; construct a new node according to the code snippet and the corresponding code snippet identifier information, and append the node to the code tree according to the located added node position.
4. The method for generating code step by step from a large language model based on a code forest agent according to any one of claims 1 to 3, characterized in that: A code forest is represented by a working directory, which consists of all the code files under the working directory.
5. The method for generating code step by step using a large language model based on a code forest agent according to claim 2 or 3, characterized in that: The generated code snippet is parsed by a parsing program to obtain the corresponding code snippet identifier information, or parsed using a large language model LLM.
6. The method for generating code step by step from a large language model based on a code forest agent according to claim 1, characterized in that: The code forest adopts a JSON structured form, including: the tree name of each code tree, the previous node and the next node of all nodes, the code fragment of each node, and the corresponding code fragment identifier information.
7. The method for generating code step by step from a large language model based on a code forest agent according to claim 6, characterized in that: The code snippet identifier information includes an identifier name, an identifier value, and an identifier description information.
8. The method for generating code step by step from a large language model based on a code forest agent according to claim 6, characterized in that: The agent automatically manages the code forest, including: first, the agent uses the get_tree tool to specify the tree name of the code tree to be obtained according to the demand, the get_tree tool searches the field name in JSON according to the tree name, obtains the code tree and returns it to the agent, the agent determines to which node position the code generated by the current demand should be added, and uses the locate_node tool to specify the position to be inserted. The locate_node tool recursively searches the previous node pre_node field in JSON according to the specified position, obtains the identifier information of all ancestor nodes until the root node of the tree, and returns the identifier information of all found ancestor nodes to the agent, the agent sends the demand and identifier information to the LLM to start generating code, the generated code is tested to be correct, and then the newly generated code fragment is parsed to obtain the corresponding code fragment identifier information; a new node is formed according to the code fragment and the corresponding code fragment identifier information, and the node is appended to this code tree according to the located added node position.
9. A device for generating codes step by step based on a large language model of a code forest agent, characterized in that: The device comprises: A code forest construction module is used to: generate and construct a code forest according to the initially generated code; wherein the code forest includes multiple code trees, one code tree represents all the code content in a code file, and organizes nodes through a tree structure, that is, organizes code fragments; each node of the code tree contains a code fragment and a code fragment identifier information; The code forest management module is used to: the Agent automatically manages the code forest, obtains code snippets and code snippet identifier information when generating code, and then appends the generated code to the code forest.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 8 are implemented.
Citation Information
Cited By
Player behavior log-based syntax tree structure automatic labeling and behavior pattern mining method and system
CN121979965A